validate.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _defaultConfig = _interopRequireDefault(require('./defaultConfig'));
  7. var _utils = require('./utils');
  8. function _interopRequireDefault(obj) {
  9. return obj && obj.__esModule ? obj : {default: obj};
  10. }
  11. function _objectSpread(target) {
  12. for (var i = 1; i < arguments.length; i++) {
  13. var source = arguments[i] != null ? arguments[i] : {};
  14. var ownKeys = Object.keys(source);
  15. if (typeof Object.getOwnPropertySymbols === 'function') {
  16. ownKeys = ownKeys.concat(
  17. Object.getOwnPropertySymbols(source).filter(function(sym) {
  18. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  19. })
  20. );
  21. }
  22. ownKeys.forEach(function(key) {
  23. _defineProperty(target, key, source[key]);
  24. });
  25. }
  26. return target;
  27. }
  28. function _defineProperty(obj, key, value) {
  29. if (key in obj) {
  30. Object.defineProperty(obj, key, {
  31. value: value,
  32. enumerable: true,
  33. configurable: true,
  34. writable: true
  35. });
  36. } else {
  37. obj[key] = value;
  38. }
  39. return obj;
  40. }
  41. let hasDeprecationWarnings = false;
  42. const shouldSkipValidationForPath = (path, key, blacklist) =>
  43. blacklist ? blacklist.includes([...path, key].join('.')) : false;
  44. const _validate = (config, exampleConfig, options, path = []) => {
  45. if (
  46. typeof config !== 'object' ||
  47. config == null ||
  48. typeof exampleConfig !== 'object' ||
  49. exampleConfig == null
  50. ) {
  51. return {
  52. hasDeprecationWarnings
  53. };
  54. }
  55. for (const key in config) {
  56. if (
  57. options.deprecatedConfig &&
  58. key in options.deprecatedConfig &&
  59. typeof options.deprecate === 'function'
  60. ) {
  61. const isDeprecatedKey = options.deprecate(
  62. config,
  63. key,
  64. options.deprecatedConfig,
  65. options
  66. );
  67. hasDeprecationWarnings = hasDeprecationWarnings || isDeprecatedKey;
  68. } else if (allowsMultipleTypes(key)) {
  69. const value = config[key];
  70. if (
  71. typeof options.condition === 'function' &&
  72. typeof options.error === 'function'
  73. ) {
  74. if (key === 'maxWorkers' && !isOfTypeStringOrNumber(value)) {
  75. throw new _utils.ValidationError(
  76. 'Validation Error',
  77. `${key} has to be of type string or number`,
  78. `maxWorkers=50% or\nmaxWorkers=3`
  79. );
  80. }
  81. }
  82. } else if (Object.hasOwnProperty.call(exampleConfig, key)) {
  83. if (
  84. typeof options.condition === 'function' &&
  85. typeof options.error === 'function' &&
  86. !options.condition(config[key], exampleConfig[key])
  87. ) {
  88. options.error(key, config[key], exampleConfig[key], options, path);
  89. }
  90. } else if (
  91. shouldSkipValidationForPath(path, key, options.recursiveBlacklist)
  92. ) {
  93. // skip validating unknown options inside blacklisted paths
  94. } else {
  95. options.unknown &&
  96. options.unknown(config, exampleConfig, key, options, path);
  97. }
  98. if (
  99. options.recursive &&
  100. !Array.isArray(exampleConfig[key]) &&
  101. options.recursiveBlacklist &&
  102. !shouldSkipValidationForPath(path, key, options.recursiveBlacklist)
  103. ) {
  104. _validate(config[key], exampleConfig[key], options, [...path, key]);
  105. }
  106. }
  107. return {
  108. hasDeprecationWarnings
  109. };
  110. };
  111. const allowsMultipleTypes = key => key === 'maxWorkers';
  112. const isOfTypeStringOrNumber = value =>
  113. typeof value === 'number' || typeof value === 'string';
  114. const validate = (config, options) => {
  115. hasDeprecationWarnings = false; // Preserve default blacklist entries even with user-supplied blacklist
  116. const combinedBlacklist = [
  117. ...(_defaultConfig.default.recursiveBlacklist || []),
  118. ...(options.recursiveBlacklist || [])
  119. ];
  120. const defaultedOptions = Object.assign(
  121. _objectSpread({}, _defaultConfig.default, options, {
  122. recursiveBlacklist: combinedBlacklist,
  123. title: options.title || _defaultConfig.default.title
  124. })
  125. );
  126. const _validate2 = _validate(config, options.exampleConfig, defaultedOptions),
  127. hdw = _validate2.hasDeprecationWarnings;
  128. return {
  129. hasDeprecationWarnings: hdw,
  130. isValid: true
  131. };
  132. };
  133. var _default = validate;
  134. exports.default = _default;