1 | 'use strict'
|
2 |
|
3 | const validate = require('./configValidator')
|
4 | const deepClone = require('rfdc')({ circles: true, proto: false })
|
5 | const { FST_ERR_INIT_OPTS_INVALID } = require('./errors')
|
6 |
|
7 | function 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 |
|
19 | function 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 |
|
35 | module.exports = validateInitialConfig
|
36 | module.exports.defaultInitOptions = validate.defaultInitOptions
|
37 | module.exports.utils = { deepFreezeObject }
|