1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | Object.defineProperty(exports, "ValidationError", {
|
7 | enumerable: true,
|
8 | get: function () {
|
9 | return _ValidationError.default;
|
10 | }
|
11 | });
|
12 | exports.disableValidation = disableValidation;
|
13 | exports.enableValidation = enableValidation;
|
14 | exports.needValidate = needValidate;
|
15 | exports.validate = validate;
|
16 | var _ValidationError = _interopRequireDefault(require("./ValidationError"));
|
17 | var _memorize = _interopRequireDefault(require("./util/memorize"));
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19 | const getAjv = (0, _memorize.default)(() => {
|
20 |
|
21 |
|
22 | const Ajv = require("ajv").default;
|
23 |
|
24 | const ajvKeywords = require("ajv-keywords").default;
|
25 |
|
26 | const addFormats = require("ajv-formats").default;
|
27 |
|
28 | |
29 |
|
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 | addFormats(ajv, {
|
39 | keywords: true
|
40 | });
|
41 |
|
42 |
|
43 |
|
44 | const addAbsolutePathKeyword = require("./keywords/absolutePath").default;
|
45 | addAbsolutePathKeyword(ajv);
|
46 | const addUndefinedAsNullKeyword =
|
47 |
|
48 | require("./keywords/undefinedAsNull").default;
|
49 | addUndefinedAsNullKeyword(ajv);
|
50 | return ajv;
|
51 | });
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | function applyPrefix(error, idx) {
|
92 |
|
93 | error.instancePath = `[${idx}]${error.instancePath}`;
|
94 | if (error.children) {
|
95 | error.children.forEach(err => applyPrefix(err, idx));
|
96 | }
|
97 | return error;
|
98 | }
|
99 | let skipValidation = false;
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function enableValidation() {
|
106 | skipValidation = false;
|
107 |
|
108 |
|
109 | if (process && process.env) {
|
110 | process.env.SKIP_VALIDATION = "n";
|
111 | }
|
112 | }
|
113 |
|
114 |
|
115 | function disableValidation() {
|
116 | skipValidation = true;
|
117 | if (process && process.env) {
|
118 | process.env.SKIP_VALIDATION = "y";
|
119 | }
|
120 | }
|
121 |
|
122 |
|
123 | function needValidate() {
|
124 | if (skipValidation) {
|
125 | return false;
|
126 | }
|
127 | if (process && process.env && process.env.SKIP_VALIDATION) {
|
128 | const value = process.env.SKIP_VALIDATION.trim();
|
129 | if (/^(?:y|yes|true|1|on)$/i.test(value)) {
|
130 | return false;
|
131 | }
|
132 | if (/^(?:n|no|false|0|off)$/i.test(value)) {
|
133 | return true;
|
134 | }
|
135 | }
|
136 | return true;
|
137 | }
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | function validate(schema, options, configuration) {
|
146 | if (!needValidate()) {
|
147 | return;
|
148 | }
|
149 | let errors = [];
|
150 | if (Array.isArray(options)) {
|
151 | for (let i = 0; i <= options.length - 1; i++) {
|
152 | errors.push(...validateObject(schema, options[i]).map(err => applyPrefix(err, i)));
|
153 | }
|
154 | } else {
|
155 | errors = validateObject(schema, options);
|
156 | }
|
157 | if (errors.length > 0) {
|
158 | throw new _ValidationError.default(errors, schema, configuration);
|
159 | }
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | function validateObject(schema, options) {
|
168 |
|
169 | const compiledSchema = getAjv().compile(schema);
|
170 | const valid = compiledSchema(options);
|
171 | if (valid) return [];
|
172 | return compiledSchema.errors ? filterErrors(compiledSchema.errors) : [];
|
173 | }
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | function filterErrors(errors) {
|
180 |
|
181 | let newErrors = [];
|
182 | for (const error of errors) {
|
183 | const {
|
184 | instancePath
|
185 | } = error;
|
186 |
|
187 | let children = [];
|
188 | newErrors = newErrors.filter(oldError => {
|
189 | if (oldError.instancePath.includes(instancePath)) {
|
190 | if (oldError.children) {
|
191 | children = children.concat(oldError.children.slice(0));
|
192 | }
|
193 |
|
194 |
|
195 | oldError.children = undefined;
|
196 | children.push(oldError);
|
197 | return false;
|
198 | }
|
199 | return true;
|
200 | });
|
201 | if (children.length) {
|
202 | error.children = children;
|
203 | }
|
204 | newErrors.push(error);
|
205 | }
|
206 | return newErrors;
|
207 | } |
\ | No newline at end of file |