source: frontend/node_modules/schema-utils/dist/validate.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 6.2 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6Object.defineProperty(exports, "ValidationError", {
7 enumerable: true,
8 get: function () {
9 return _ValidationError.default;
10 }
11});
12exports.disableValidation = disableValidation;
13exports.enableValidation = enableValidation;
14exports.needValidate = needValidate;
15exports.validate = validate;
16var _ValidationError = _interopRequireDefault(require("./ValidationError"));
17var _memorize = _interopRequireDefault(require("./util/memorize"));
18function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
19const getAjv = (0, _memorize.default)(() => {
20 // Use CommonJS require for ajv libs so TypeScript consumers aren't locked into esModuleInterop (see #110).
21
22 const Ajv = require("ajv").default;
23 const ajvKeywords = require("ajv-keywords").default;
24 const addFormats = require("ajv-formats").default;
25
26 /**
27 * @type {Ajv}
28 */
29 const ajv = new Ajv({
30 strict: false,
31 allErrors: true,
32 verbose: true,
33 $data: true
34 });
35 ajvKeywords(ajv, ["instanceof", "patternRequired"]);
36 // TODO set `{ keywords: true }` for the next major release and remove `keywords/limit.js`
37 addFormats(ajv, {
38 keywords: false
39 });
40
41 // Custom keywords
42
43 const addAbsolutePathKeyword = require("./keywords/absolutePath").default;
44 addAbsolutePathKeyword(ajv);
45 const addLimitKeyword = require("./keywords/limit").default;
46 addLimitKeyword(ajv);
47 const addUndefinedAsNullKeyword = require("./keywords/undefinedAsNull").default;
48 addUndefinedAsNullKeyword(ajv);
49 return ajv;
50});
51
52/** @typedef {import("json-schema").JSONSchema4} JSONSchema4 */
53/** @typedef {import("json-schema").JSONSchema6} JSONSchema6 */
54/** @typedef {import("json-schema").JSONSchema7} JSONSchema7 */
55/** @typedef {import("ajv").ErrorObject} ErrorObject */
56
57/**
58 * @typedef {object} ExtendedSchema
59 * @property {(string | number)=} formatMinimum format minimum
60 * @property {(string | number)=} formatMaximum format maximum
61 * @property {(string | boolean)=} formatExclusiveMinimum format exclusive minimum
62 * @property {(string | boolean)=} formatExclusiveMaximum format exclusive maximum
63 * @property {string=} link link
64 * @property {boolean=} undefinedAsNull undefined will be resolved as null
65 */
66
67// TODO remove me in the next major release
68/** @typedef {ExtendedSchema} Extend */
69
70/** @typedef {(JSONSchema4 | JSONSchema6 | JSONSchema7) & ExtendedSchema} Schema */
71
72/** @typedef {ErrorObject & { children?: Array<ErrorObject> }} SchemaUtilErrorObject */
73
74/**
75 * @callback PostFormatter
76 * @param {string} formattedError
77 * @param {SchemaUtilErrorObject} error
78 * @returns {string}
79 */
80
81/**
82 * @typedef {object} ValidationErrorConfiguration
83 * @property {string=} name name
84 * @property {string=} baseDataPath base data path
85 * @property {PostFormatter=} postFormatter post formatter
86 */
87
88/**
89 * @param {SchemaUtilErrorObject} error error
90 * @param {number} idx idx
91 * @returns {SchemaUtilErrorObject} error object with idx
92 */
93function applyPrefix(error, idx) {
94 error.instancePath = `[${idx}]${error.instancePath}`;
95 if (error.children) {
96 for (const err of error.children) applyPrefix(err, idx);
97 }
98 return error;
99}
100let skipValidation = false;
101
102// We use `process.env.SKIP_VALIDATION` because you can have multiple `schema-utils` with different version,
103// so we want to disable it globally, `process.env` doesn't supported by browsers, so we have the local `skipValidation` variables
104
105// Enable validation
106/**
107 * @returns {void}
108 */
109function enableValidation() {
110 skipValidation = false;
111
112 // Disable validation for any versions
113 if (process && process.env) {
114 process.env.SKIP_VALIDATION = "n";
115 }
116}
117
118// Disable validation
119/**
120 * @returns {void}
121 */
122function disableValidation() {
123 skipValidation = true;
124 if (process && process.env) {
125 process.env.SKIP_VALIDATION = "y";
126 }
127}
128
129// Check if we need to confirm
130/**
131 * @returns {boolean} true when need validate, otherwise false
132 */
133function needValidate() {
134 if (skipValidation) {
135 return false;
136 }
137 if (process && process.env && process.env.SKIP_VALIDATION) {
138 const value = process.env.SKIP_VALIDATION.trim();
139 if (/^(?:y|yes|true|1|on)$/i.test(value)) {
140 return false;
141 }
142 if (/^(?:n|no|false|0|off)$/i.test(value)) {
143 return true;
144 }
145 }
146 return true;
147}
148
149/**
150 * @param {Array<ErrorObject>} errors array of error objects
151 * @returns {Array<SchemaUtilErrorObject>} filtered array of objects
152 */
153function filterErrors(errors) {
154 /** @type {Array<SchemaUtilErrorObject>} */
155 let newErrors = [];
156 for (const error of (/** @type {Array<SchemaUtilErrorObject>} */errors)) {
157 const {
158 instancePath
159 } = error;
160 /** @type {Array<SchemaUtilErrorObject>} */
161 let children = [];
162 newErrors = newErrors.filter(oldError => {
163 if (oldError.instancePath.includes(instancePath)) {
164 if (oldError.children) {
165 children = [...children, ...oldError.children];
166 }
167 oldError.children = undefined;
168 children.push(oldError);
169 return false;
170 }
171 return true;
172 });
173 if (children.length) {
174 error.children = children;
175 }
176 newErrors.push(error);
177 }
178 return newErrors;
179}
180
181/**
182 * @param {Schema} schema schema
183 * @param {Array<object> | object} options options
184 * @returns {Array<SchemaUtilErrorObject>} array of error objects
185 */
186function validateObject(schema, options) {
187 // Not need to cache, because `ajv@8` has built-in cache
188 const compiledSchema = getAjv().compile(schema);
189 const valid = compiledSchema(options);
190 if (valid) return [];
191 return compiledSchema.errors ? filterErrors(compiledSchema.errors) : [];
192}
193
194/**
195 * @param {Schema} schema schema
196 * @param {Array<object> | object} options options
197 * @param {ValidationErrorConfiguration=} configuration configuration
198 * @returns {void}
199 */
200function validate(schema, options, configuration) {
201 if (!needValidate()) {
202 return;
203 }
204 let errors = [];
205 if (Array.isArray(options)) {
206 for (let i = 0; i <= options.length - 1; i++) {
207 errors.push(...validateObject(schema, options[i]).map(err => applyPrefix(err, i)));
208 }
209 } else {
210 errors = validateObject(schema, options);
211 }
212 if (errors.length > 0) {
213 throw new _ValidationError.default(errors, schema, configuration);
214 }
215}
Note: See TracBrowser for help on using the repository browser.