1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 | var _defaultConfig = _interopRequireDefault(require('./defaultConfig'));
|
8 | var _utils = require('./utils');
|
9 | function _interopRequireDefault(obj) {
|
10 | return obj && obj.__esModule ? obj : {default: obj};
|
11 | }
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | let hasDeprecationWarnings = false;
|
20 | const shouldSkipValidationForPath = (path, key, denylist) =>
|
21 | denylist ? denylist.includes([...path, key].join('.')) : false;
|
22 | const _validate = (config, exampleConfig, options, path = []) => {
|
23 | if (
|
24 | typeof config !== 'object' ||
|
25 | config == null ||
|
26 | typeof exampleConfig !== 'object' ||
|
27 | exampleConfig == null
|
28 | ) {
|
29 | return {
|
30 | hasDeprecationWarnings
|
31 | };
|
32 | }
|
33 | for (const key in config) {
|
34 | if (
|
35 | options.deprecatedConfig &&
|
36 | key in options.deprecatedConfig &&
|
37 | typeof options.deprecate === 'function'
|
38 | ) {
|
39 | const isDeprecatedKey = options.deprecate(
|
40 | config,
|
41 | key,
|
42 | options.deprecatedConfig,
|
43 | options
|
44 | );
|
45 | hasDeprecationWarnings = hasDeprecationWarnings || isDeprecatedKey;
|
46 | } else if (allowsMultipleTypes(key)) {
|
47 | const value = config[key];
|
48 | if (
|
49 | typeof options.condition === 'function' &&
|
50 | typeof options.error === 'function'
|
51 | ) {
|
52 | if (key === 'maxWorkers' && !isOfTypeStringOrNumber(value)) {
|
53 | throw new _utils.ValidationError(
|
54 | 'Validation Error',
|
55 | `${key} has to be of type string or number`,
|
56 | 'maxWorkers=50% or\nmaxWorkers=3'
|
57 | );
|
58 | }
|
59 | }
|
60 | } else if (Object.hasOwnProperty.call(exampleConfig, key)) {
|
61 | if (
|
62 | typeof options.condition === 'function' &&
|
63 | typeof options.error === 'function' &&
|
64 | !options.condition(config[key], exampleConfig[key])
|
65 | ) {
|
66 | options.error(key, config[key], exampleConfig[key], options, path);
|
67 | }
|
68 | } else if (
|
69 | shouldSkipValidationForPath(path, key, options.recursiveDenylist)
|
70 | ) {
|
71 |
|
72 | } else {
|
73 | options.unknown &&
|
74 | options.unknown(config, exampleConfig, key, options, path);
|
75 | }
|
76 | if (
|
77 | options.recursive &&
|
78 | !Array.isArray(exampleConfig[key]) &&
|
79 | options.recursiveDenylist &&
|
80 | !shouldSkipValidationForPath(path, key, options.recursiveDenylist)
|
81 | ) {
|
82 | _validate(config[key], exampleConfig[key], options, [...path, key]);
|
83 | }
|
84 | }
|
85 | return {
|
86 | hasDeprecationWarnings
|
87 | };
|
88 | };
|
89 | const allowsMultipleTypes = key => key === 'maxWorkers';
|
90 | const isOfTypeStringOrNumber = value =>
|
91 | typeof value === 'number' || typeof value === 'string';
|
92 | const validate = (config, options) => {
|
93 | hasDeprecationWarnings = false;
|
94 |
|
95 |
|
96 | const combinedDenylist = [
|
97 | ...(_defaultConfig.default.recursiveDenylist || []),
|
98 | ...(options.recursiveDenylist || [])
|
99 | ];
|
100 | const defaultedOptions = Object.assign({
|
101 | ..._defaultConfig.default,
|
102 | ...options,
|
103 | recursiveDenylist: combinedDenylist,
|
104 | title: options.title || _defaultConfig.default.title
|
105 | });
|
106 | const {hasDeprecationWarnings: hdw} = _validate(
|
107 | config,
|
108 | options.exampleConfig,
|
109 | defaultedOptions
|
110 | );
|
111 | return {
|
112 | hasDeprecationWarnings: hdw,
|
113 | isValid: true
|
114 | };
|
115 | };
|
116 | var _default = validate;
|
117 | exports.default = _default;
|