validate.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _ajv = _interopRequireDefault(require("ajv"));
  7. var _ajvKeywords = _interopRequireDefault(require("ajv-keywords"));
  8. var _absolutePath = _interopRequireDefault(require("./keywords/absolutePath"));
  9. var _ValidationError = _interopRequireDefault(require("./ValidationError"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /** @typedef {import("json-schema").JSONSchema4} JSONSchema4 */
  12. /** @typedef {import("json-schema").JSONSchema6} JSONSchema6 */
  13. /** @typedef {import("json-schema").JSONSchema7} JSONSchema7 */
  14. /** @typedef {import("ajv").ErrorObject} ErrorObject */
  15. /**
  16. * @typedef {Object} Extend
  17. * @property {number=} formatMinimum
  18. * @property {number=} formatMaximum
  19. * @property {boolean=} formatExclusiveMinimum
  20. * @property {boolean=} formatExclusiveMaximum
  21. */
  22. /** @typedef {(JSONSchema4 | JSONSchema6 | JSONSchema7) & Extend} Schema */
  23. /** @typedef {ErrorObject & { children?: Array<ErrorObject>}} SchemaUtilErrorObject */
  24. /**
  25. * @callback PostFormatter
  26. * @param {string} formattedError
  27. * @param {SchemaUtilErrorObject} error
  28. * @returns {string}
  29. */
  30. /**
  31. * @typedef {Object} ValidationErrorConfiguration
  32. * @property {string=} name
  33. * @property {string=} baseDataPath
  34. * @property {PostFormatter=} postFormatter
  35. */
  36. const ajv = new _ajv.default({
  37. allErrors: true,
  38. verbose: true,
  39. $data: true
  40. });
  41. (0, _ajvKeywords.default)(ajv, ['instanceof', 'formatMinimum', 'formatMaximum', 'patternRequired']); // Custom keywords
  42. (0, _absolutePath.default)(ajv);
  43. /**
  44. * @param {Schema} schema
  45. * @param {Array<object> | object} options
  46. * @param {ValidationErrorConfiguration=} configuration
  47. * @returns {void}
  48. */
  49. function validate(schema, options, configuration) {
  50. let errors = [];
  51. if (Array.isArray(options)) {
  52. errors = Array.from(options, nestedOptions => validateObject(schema, nestedOptions));
  53. errors.forEach((list, idx) => {
  54. const applyPrefix =
  55. /**
  56. * @param {SchemaUtilErrorObject} error
  57. */
  58. error => {
  59. // eslint-disable-next-line no-param-reassign
  60. error.dataPath = `[${idx}]${error.dataPath}`;
  61. if (error.children) {
  62. error.children.forEach(applyPrefix);
  63. }
  64. };
  65. list.forEach(applyPrefix);
  66. });
  67. errors = errors.reduce((arr, items) => {
  68. arr.push(...items);
  69. return arr;
  70. }, []);
  71. } else {
  72. errors = validateObject(schema, options);
  73. }
  74. if (errors.length > 0) {
  75. throw new _ValidationError.default(errors, schema, configuration);
  76. }
  77. }
  78. /**
  79. * @param {Schema} schema
  80. * @param {Array<object> | object} options
  81. * @returns {Array<SchemaUtilErrorObject>}
  82. */
  83. function validateObject(schema, options) {
  84. const compiledSchema = ajv.compile(schema);
  85. const valid = compiledSchema(options);
  86. if (valid) return [];
  87. return compiledSchema.errors ? filterErrors(compiledSchema.errors) : [];
  88. }
  89. /**
  90. * @param {Array<ErrorObject>} errors
  91. * @returns {Array<SchemaUtilErrorObject>}
  92. */
  93. function filterErrors(errors) {
  94. /** @type {Array<SchemaUtilErrorObject>} */
  95. let newErrors = [];
  96. for (const error of
  97. /** @type {Array<SchemaUtilErrorObject>} */
  98. errors) {
  99. const {
  100. dataPath
  101. } = error;
  102. /** @type {Array<SchemaUtilErrorObject>} */
  103. let children = [];
  104. newErrors = newErrors.filter(oldError => {
  105. if (oldError.dataPath.includes(dataPath)) {
  106. if (oldError.children) {
  107. children = children.concat(oldError.children.slice(0));
  108. } // eslint-disable-next-line no-undefined, no-param-reassign
  109. oldError.children = undefined;
  110. children.push(oldError);
  111. return false;
  112. }
  113. return true;
  114. });
  115. if (children.length) {
  116. error.children = children;
  117. }
  118. newErrors.push(error);
  119. }
  120. return newErrors;
  121. } // TODO change after resolve https://github.com/microsoft/TypeScript/issues/34994
  122. validate.ValidationError = _ValidationError.default;
  123. validate.ValidateError = _ValidationError.default;
  124. var _default = validate;
  125. exports.default = _default;