source: trip-planner-front/node_modules/webpack-dev-middleware/node_modules/schema-utils/dist/validate.js@ fa375fe

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

initial commit

  • Property mode set to 100644
File size: 4.0 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.validate = validate;
7Object.defineProperty(exports, "ValidationError", {
8 enumerable: true,
9 get: function () {
10 return _ValidationError.default;
11 }
12});
13
14var _absolutePath = _interopRequireDefault(require("./keywords/absolutePath"));
15
16var _ValidationError = _interopRequireDefault(require("./ValidationError"));
17
18function _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).
21const Ajv = require("ajv");
22
23const 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
60const ajv = new Ajv({
61 allErrors: true,
62 verbose: true,
63 $data: true
64});
65ajvKeywords(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
75function 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
115function 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
127function 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}
Note: See TracBrowser for help on using the repository browser.