validate.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. var _default_config;
  6. function _load_default_config() {
  7. return (_default_config = _interopRequireDefault(
  8. require('./default_config')
  9. ));
  10. }
  11. function _interopRequireDefault(obj) {
  12. return obj && obj.__esModule ? obj : {default: obj};
  13. }
  14. function _toConsumableArray(arr) {
  15. if (Array.isArray(arr)) {
  16. for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++)
  17. arr2[i] = arr[i];
  18. return arr2;
  19. } else {
  20. return Array.from(arr);
  21. }
  22. }
  23. /**
  24. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  25. *
  26. * This source code is licensed under the MIT license found in the
  27. * LICENSE file in the root directory of this source tree.
  28. *
  29. *
  30. */
  31. let hasDeprecationWarnings = false;
  32. const shouldSkipValidationForPath = (path, key, blacklist) =>
  33. blacklist
  34. ? blacklist.includes([].concat(_toConsumableArray(path), [key]).join('.'))
  35. : false;
  36. const _validate = function(config, exampleConfig, options) {
  37. let path =
  38. arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
  39. if (
  40. typeof config !== 'object' ||
  41. config == null ||
  42. typeof exampleConfig !== 'object' ||
  43. exampleConfig == null
  44. ) {
  45. return {hasDeprecationWarnings: hasDeprecationWarnings};
  46. }
  47. for (const key in config) {
  48. if (
  49. options.deprecatedConfig &&
  50. key in options.deprecatedConfig &&
  51. typeof options.deprecate === 'function'
  52. ) {
  53. const isDeprecatedKey = options.deprecate(
  54. config,
  55. key,
  56. options.deprecatedConfig,
  57. options
  58. );
  59. hasDeprecationWarnings = hasDeprecationWarnings || isDeprecatedKey;
  60. } else if (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.recursiveBlacklist)
  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.recursiveBlacklist &&
  80. !shouldSkipValidationForPath(path, key, options.recursiveBlacklist)
  81. ) {
  82. _validate(
  83. config[key],
  84. exampleConfig[key],
  85. options,
  86. [].concat(_toConsumableArray(path), [key])
  87. );
  88. }
  89. }
  90. return {hasDeprecationWarnings: hasDeprecationWarnings};
  91. };
  92. const validate = (config, options) => {
  93. hasDeprecationWarnings = false;
  94. const defaultedOptions = Object.assign(
  95. {},
  96. (_default_config || _load_default_config()).default,
  97. options,
  98. {
  99. title: Object.assign(
  100. {},
  101. (_default_config || _load_default_config()).default.title,
  102. options.title
  103. )
  104. }
  105. );
  106. var _validate2 = _validate(config, options.exampleConfig, defaultedOptions);
  107. const hdw = _validate2.hasDeprecationWarnings;
  108. return {
  109. hasDeprecationWarnings: hdw,
  110. isValid: true
  111. };
  112. };
  113. exports.default = validate;