1 | 'use strict'
|
2 |
|
3 | const validate = require('./configValidator')
|
4 | const deepClone = require('rfdc')({ circles: true, proto: false })
|
5 | const {
|
6 | codes: {
|
7 | FST_ERR_INIT_OPTS_INVALID
|
8 | }
|
9 | } = require('./errors')
|
10 |
|
11 | function 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 |
|
23 | function 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 |
|
39 | module.exports = validateInitialConfig
|
40 | module.exports.defaultInitOptions = validate.defaultInitOptions
|
41 | module.exports.utils = { deepFreezeObject }
|