source: imaps-frontend/node_modules/schema-utils/dist/validate.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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