validate.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * @property {string=} link
  29. */
  30. /** @typedef {(JSONSchema4 | JSONSchema6 | JSONSchema7) & Extend} Schema */
  31. /** @typedef {ErrorObject & { children?: Array<ErrorObject>}} SchemaUtilErrorObject */
  32. /**
  33. * @callback PostFormatter
  34. * @param {string} formattedError
  35. * @param {SchemaUtilErrorObject} error
  36. * @returns {string}
  37. */
  38. /**
  39. * @typedef {Object} ValidationErrorConfiguration
  40. * @property {string=} name
  41. * @property {string=} baseDataPath
  42. * @property {PostFormatter=} postFormatter
  43. */
  44. const ajv = new Ajv({
  45. allErrors: true,
  46. verbose: true,
  47. $data: true
  48. });
  49. ajvKeywords(ajv, ["instanceof", "formatMinimum", "formatMaximum", "patternRequired"]); // Custom keywords
  50. (0, _absolutePath.default)(ajv);
  51. /**
  52. * @param {Schema} schema
  53. * @param {Array<object> | object} options
  54. * @param {ValidationErrorConfiguration=} configuration
  55. * @returns {void}
  56. */
  57. function validate(schema, options, configuration) {
  58. let errors = [];
  59. if (Array.isArray(options)) {
  60. errors = Array.from(options, nestedOptions => validateObject(schema, nestedOptions));
  61. errors.forEach((list, idx) => {
  62. const applyPrefix =
  63. /**
  64. * @param {SchemaUtilErrorObject} error
  65. */
  66. error => {
  67. // eslint-disable-next-line no-param-reassign
  68. error.dataPath = `[${idx}]${error.dataPath}`;
  69. if (error.children) {
  70. error.children.forEach(applyPrefix);
  71. }
  72. };
  73. list.forEach(applyPrefix);
  74. });
  75. errors = errors.reduce((arr, items) => {
  76. arr.push(...items);
  77. return arr;
  78. }, []);
  79. } else {
  80. errors = validateObject(schema, options);
  81. }
  82. if (errors.length > 0) {
  83. throw new _ValidationError.default(errors, schema, configuration);
  84. }
  85. }
  86. /**
  87. * @param {Schema} schema
  88. * @param {Array<object> | object} options
  89. * @returns {Array<SchemaUtilErrorObject>}
  90. */
  91. function validateObject(schema, options) {
  92. const compiledSchema = ajv.compile(schema);
  93. const valid = compiledSchema(options);
  94. if (valid) return [];
  95. return compiledSchema.errors ? filterErrors(compiledSchema.errors) : [];
  96. }
  97. /**
  98. * @param {Array<ErrorObject>} errors
  99. * @returns {Array<SchemaUtilErrorObject>}
  100. */
  101. function filterErrors(errors) {
  102. /** @type {Array<SchemaUtilErrorObject>} */
  103. let newErrors = [];
  104. for (const error of
  105. /** @type {Array<SchemaUtilErrorObject>} */
  106. errors) {
  107. const {
  108. dataPath
  109. } = error;
  110. /** @type {Array<SchemaUtilErrorObject>} */
  111. let children = [];
  112. newErrors = newErrors.filter(oldError => {
  113. if (oldError.dataPath.includes(dataPath)) {
  114. if (oldError.children) {
  115. children = children.concat(oldError.children.slice(0));
  116. } // eslint-disable-next-line no-undefined, no-param-reassign
  117. oldError.children = undefined;
  118. children.push(oldError);
  119. return false;
  120. }
  121. return true;
  122. });
  123. if (children.length) {
  124. error.children = children;
  125. }
  126. newErrors.push(error);
  127. }
  128. return newErrors;
  129. }