UNPKG

3.57 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = void 0;
7var _defaultConfig = _interopRequireDefault(require('./defaultConfig'));
8var _utils = require('./utils');
9function _interopRequireDefault(obj) {
10 return obj && obj.__esModule ? obj : {default: obj};
11}
12/**
13 * Copyright (c) Meta Platforms, Inc. and affiliates.
14 *
15 * This source code is licensed under the MIT license found in the
16 * LICENSE file in the root directory of this source tree.
17 */
18
19let hasDeprecationWarnings = false;
20const shouldSkipValidationForPath = (path, key, denylist) =>
21 denylist ? denylist.includes([...path, key].join('.')) : false;
22const _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 // skip validating unknown options inside blacklisted paths
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};
89const allowsMultipleTypes = key => key === 'maxWorkers';
90const isOfTypeStringOrNumber = value =>
91 typeof value === 'number' || typeof value === 'string';
92const validate = (config, options) => {
93 hasDeprecationWarnings = false;
94
95 // Preserve default denylist entries even with user-supplied denylist
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};
116var _default = validate;
117exports.default = _default;