validate.js 4.1 KB

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