validate.js 4.0 KB

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