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