UNPKG

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