source: trip-planner-front/node_modules/schema-utils/dist/validate.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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