validate.js 4.1 KB

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