validate.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.validate = validate;
  6. exports.enableValidation = enableValidation;
  7. exports.disableValidation = disableValidation;
  8. exports.needValidate = needValidate;
  9. Object.defineProperty(exports, "ValidationError", {
  10. enumerable: true,
  11. get: function () {
  12. return _ValidationError.default;
  13. }
  14. });
  15. var _absolutePath = _interopRequireDefault(require("./keywords/absolutePath"));
  16. var _undefinedAsNull = _interopRequireDefault(require("./keywords/undefinedAsNull"));
  17. var _ValidationError = _interopRequireDefault(require("./ValidationError"));
  18. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  19. /**
  20. * @template T
  21. * @param fn {(function(): any) | undefined}
  22. * @returns {function(): T}
  23. */
  24. const memoize = fn => {
  25. let cache = false;
  26. /** @type {T} */
  27. let result;
  28. return () => {
  29. if (cache) {
  30. return result;
  31. }
  32. result =
  33. /** @type {function(): any} */
  34. fn();
  35. cache = true; // Allow to clean up memory for fn
  36. // and all dependent resources
  37. // eslint-disable-next-line no-undefined, no-param-reassign
  38. fn = undefined;
  39. return result;
  40. };
  41. };
  42. const getAjv = memoize(() => {
  43. // Use CommonJS require for ajv libs so TypeScript consumers aren't locked into esModuleInterop (see #110).
  44. // eslint-disable-next-line global-require
  45. const Ajv = require("ajv"); // eslint-disable-next-line global-require
  46. const ajvKeywords = require("ajv-keywords");
  47. const ajv = new Ajv({
  48. allErrors: true,
  49. verbose: true,
  50. $data: true
  51. });
  52. ajvKeywords(ajv, ["instanceof", "formatMinimum", "formatMaximum", "patternRequired"]); // Custom keywords
  53. (0, _absolutePath.default)(ajv);
  54. (0, _undefinedAsNull.default)(ajv);
  55. return ajv;
  56. });
  57. /** @typedef {import("json-schema").JSONSchema4} JSONSchema4 */
  58. /** @typedef {import("json-schema").JSONSchema6} JSONSchema6 */
  59. /** @typedef {import("json-schema").JSONSchema7} JSONSchema7 */
  60. /** @typedef {import("ajv").ErrorObject} ErrorObject */
  61. /** @typedef {import("ajv").ValidateFunction} ValidateFunction */
  62. /**
  63. * @typedef {Object} Extend
  64. * @property {number=} formatMinimum
  65. * @property {number=} formatMaximum
  66. * @property {boolean=} formatExclusiveMinimum
  67. * @property {boolean=} formatExclusiveMaximum
  68. * @property {string=} link
  69. * @property {boolean=} undefinedAsNull
  70. */
  71. /** @typedef {(JSONSchema4 | JSONSchema6 | JSONSchema7) & Extend} Schema */
  72. /** @typedef {ErrorObject & { children?: Array<ErrorObject>}} SchemaUtilErrorObject */
  73. /**
  74. * @callback PostFormatter
  75. * @param {string} formattedError
  76. * @param {SchemaUtilErrorObject} error
  77. * @returns {string}
  78. */
  79. /**
  80. * @typedef {Object} ValidationErrorConfiguration
  81. * @property {string=} name
  82. * @property {string=} baseDataPath
  83. * @property {PostFormatter=} postFormatter
  84. */
  85. /**
  86. * @param {SchemaUtilErrorObject} error
  87. * @param {number} idx
  88. * @returns {SchemaUtilErrorObject}
  89. */
  90. function applyPrefix(error, idx) {
  91. // eslint-disable-next-line no-param-reassign
  92. error.dataPath = `[${idx}]${error.dataPath}`;
  93. if (error.children) {
  94. error.children.forEach(err => applyPrefix(err, idx));
  95. }
  96. return error;
  97. }
  98. let skipValidation = false; // We use `process.env.SKIP_VALIDATION` because you can have multiple `schema-utils` with different version,
  99. // so we want to disable it globally, `process.env` doesn't supported by browsers, so we have the local `skipValidation` variables
  100. // Enable validation
  101. function enableValidation() {
  102. skipValidation = false; // Disable validation for any versions
  103. if (process && process.env) {
  104. process.env.SKIP_VALIDATION = "n";
  105. }
  106. } // Disable validation
  107. function disableValidation() {
  108. skipValidation = true;
  109. if (process && process.env) {
  110. process.env.SKIP_VALIDATION = "y";
  111. }
  112. } // Check if we need to confirm
  113. function needValidate() {
  114. if (skipValidation) {
  115. return false;
  116. }
  117. if (process && process.env && process.env.SKIP_VALIDATION) {
  118. const value = process.env.SKIP_VALIDATION.trim();
  119. if (/^(?:y|yes|true|1|on)$/i.test(value)) {
  120. return false;
  121. }
  122. if (/^(?:n|no|false|0|off)$/i.test(value)) {
  123. return true;
  124. }
  125. }
  126. return true;
  127. }
  128. /**
  129. * @param {Schema} schema
  130. * @param {Array<object> | object} options
  131. * @param {ValidationErrorConfiguration=} configuration
  132. * @returns {void}
  133. */
  134. function validate(schema, options, configuration) {
  135. if (!needValidate()) {
  136. return;
  137. }
  138. let errors = [];
  139. if (Array.isArray(options)) {
  140. for (let i = 0; i <= options.length - 1; i++) {
  141. errors.push(...validateObject(schema, options[i]).map(err => applyPrefix(err, i)));
  142. }
  143. } else {
  144. errors = validateObject(schema, options);
  145. }
  146. if (errors.length > 0) {
  147. throw new _ValidationError.default(errors, schema, configuration);
  148. }
  149. }
  150. /** @typedef {WeakMap<Schema, ValidateFunction>} */
  151. const schemaCache = new WeakMap();
  152. /**
  153. * @param {Schema} schema
  154. * @param {Array<object> | object} options
  155. * @returns {Array<SchemaUtilErrorObject>}
  156. */
  157. function validateObject(schema, options) {
  158. let compiledSchema = schemaCache.get(schema);
  159. if (!compiledSchema) {
  160. compiledSchema = getAjv().compile(schema);
  161. schemaCache.set(schema, compiledSchema);
  162. }
  163. const valid = compiledSchema(options);
  164. if (valid) return [];
  165. return compiledSchema.errors ? filterErrors(compiledSchema.errors) : [];
  166. }
  167. /**
  168. * @param {Array<ErrorObject>} errors
  169. * @returns {Array<SchemaUtilErrorObject>}
  170. */
  171. function filterErrors(errors) {
  172. /** @type {Array<SchemaUtilErrorObject>} */
  173. let newErrors = [];
  174. for (const error of
  175. /** @type {Array<SchemaUtilErrorObject>} */
  176. errors) {
  177. const {
  178. dataPath
  179. } = error;
  180. /** @type {Array<SchemaUtilErrorObject>} */
  181. let children = [];
  182. newErrors = newErrors.filter(oldError => {
  183. if (oldError.dataPath.includes(dataPath)) {
  184. if (oldError.children) {
  185. children = children.concat(oldError.children.slice(0));
  186. } // eslint-disable-next-line no-undefined, no-param-reassign
  187. oldError.children = undefined;
  188. children.push(oldError);
  189. return false;
  190. }
  191. return true;
  192. });
  193. if (children.length) {
  194. error.children = children;
  195. }
  196. newErrors.push(error);
  197. }
  198. return newErrors;
  199. }