UNPKG

1.03 kBJavaScriptView Raw
1'use strict'
2
3const validate = require('./configValidator')
4const deepClone = require('rfdc')({ circles: true, proto: false })
5const {
6 codes: {
7 FST_ERR_INIT_OPTS_INVALID
8 }
9} = require('./errors')
10
11function validateInitialConfig (options) {
12 const opts = deepClone(options)
13
14 if (!validate(opts)) {
15 const error = new FST_ERR_INIT_OPTS_INVALID(JSON.stringify(validate.errors.map(e => e.message)))
16 error.errors = validate.errors
17 throw error
18 }
19
20 return deepFreezeObject(opts)
21}
22
23function deepFreezeObject (object) {
24 const properties = Object.getOwnPropertyNames(object)
25
26 for (const name of properties) {
27 const value = object[name]
28
29 if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
30 continue
31 }
32
33 object[name] = value && typeof value === 'object' ? deepFreezeObject(value) : value
34 }
35
36 return Object.freeze(object)
37}
38
39module.exports = validateInitialConfig
40module.exports.defaultInitOptions = validate.defaultInitOptions
41module.exports.utils = { deepFreezeObject }