BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / ajv / lib / dotjs / properties.js

properties.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
properties.js 21.3 KiB Download Raw
1'use strict';
2module.exports = function generate_properties(it, $keyword, $ruleType) {
3 var out = ' ';
4 var $lvl = it.level;
5 var $dataLvl = it.dataLevel;
6 var $schema = it.schema[$keyword];
7 var $schemaPath = it.schemaPath + it.util.getProperty($keyword);
8 var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
9 var $breakOnError = !it.opts.allErrors;
10 var $data = 'data' + ($dataLvl || '');
11 var $valid = 'valid' + $lvl;
12 var $errs = 'errs__' + $lvl;
13 var $it = it.util.copy(it);
14 var $closingBraces = '';
15 $it.level++;
16 var $nextValid = 'valid' + $it.level;
17 var $key = 'key' + $lvl,
18 $idx = 'idx' + $lvl,
19 $dataNxt = $it.dataLevel = it.dataLevel + 1,
20 $nextData = 'data' + $dataNxt,
21 $dataProperties = 'dataProperties' + $lvl;
22 var $schemaKeys = Object.keys($schema || {}),
23 $pProperties = it.schema.patternProperties || {},
24 $pPropertyKeys = Object.keys($pProperties),
25 $aProperties = it.schema.additionalProperties,
26 $someProperties = $schemaKeys.length || $pPropertyKeys.length,
27 $noAdditional = $aProperties === false,
28 $additionalIsSchema = typeof $aProperties == 'object' && Object.keys($aProperties).length,
29 $removeAdditional = it.opts.removeAdditional,
30 $checkAdditional = $noAdditional || $additionalIsSchema || $removeAdditional,
31 $ownProperties = it.opts.ownProperties,
32 $currentBaseId = it.baseId;
33 var $required = it.schema.required;
34 if ($required && !(it.opts.v5 && $required.$data) && $required.length < it.opts.loopRequired) var $requiredHash = it.util.toHash($required);
35 if (it.opts.patternGroups) {
36 var $pgProperties = it.schema.patternGroups || {},
37 $pgPropertyKeys = Object.keys($pgProperties);
38 }
39 out += 'var ' + ($errs) + ' = errors;var ' + ($nextValid) + ' = true;';
40 if ($ownProperties) {
41 out += ' var ' + ($dataProperties) + ' = undefined;';
42 }
43 if ($checkAdditional) {
44 if ($ownProperties) {
45 out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
46 } else {
47 out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
48 }
49 if ($someProperties) {
50 out += ' var isAdditional' + ($lvl) + ' = !(false ';
51 if ($schemaKeys.length) {
52 if ($schemaKeys.length > 5) {
53 out += ' || validate.schema' + ($schemaPath) + '[' + ($key) + '] ';
54 } else {
55 var arr1 = $schemaKeys;
56 if (arr1) {
57 var $propertyKey, i1 = -1,
58 l1 = arr1.length - 1;
59 while (i1 < l1) {
60 $propertyKey = arr1[i1 += 1];
61 out += ' || ' + ($key) + ' == ' + (it.util.toQuotedString($propertyKey)) + ' ';
62 }
63 }
64 }
65 }
66 if ($pPropertyKeys.length) {
67 var arr2 = $pPropertyKeys;
68 if (arr2) {
69 var $pProperty, $i = -1,
70 l2 = arr2.length - 1;
71 while ($i < l2) {
72 $pProperty = arr2[$i += 1];
73 out += ' || ' + (it.usePattern($pProperty)) + '.test(' + ($key) + ') ';
74 }
75 }
76 }
77 if (it.opts.patternGroups && $pgPropertyKeys.length) {
78 var arr3 = $pgPropertyKeys;
79 if (arr3) {
80 var $pgProperty, $i = -1,
81 l3 = arr3.length - 1;
82 while ($i < l3) {
83 $pgProperty = arr3[$i += 1];
84 out += ' || ' + (it.usePattern($pgProperty)) + '.test(' + ($key) + ') ';
85 }
86 }
87 }
88 out += ' ); if (isAdditional' + ($lvl) + ') { ';
89 }
90 if ($removeAdditional == 'all') {
91 out += ' delete ' + ($data) + '[' + ($key) + ']; ';
92 } else {
93 var $currentErrorPath = it.errorPath;
94 var $additionalProperty = '\' + ' + $key + ' + \'';
95 if (it.opts._errorDataPathProperty) {
96 it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
97 }
98 if ($noAdditional) {
99 if ($removeAdditional) {
100 out += ' delete ' + ($data) + '[' + ($key) + ']; ';
101 } else {
102 out += ' ' + ($nextValid) + ' = false; ';
103 var $currErrSchemaPath = $errSchemaPath;
104 $errSchemaPath = it.errSchemaPath + '/additionalProperties';
105 var $$outStack = $$outStack || [];
106 $$outStack.push(out);
107 out = ''; /* istanbul ignore else */
108 if (it.createErrors !== false) {
109 out += ' { keyword: \'' + ('additionalProperties') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { additionalProperty: \'' + ($additionalProperty) + '\' } ';
110 if (it.opts.messages !== false) {
111 out += ' , message: \'should NOT have additional properties\' ';
112 }
113 if (it.opts.verbose) {
114 out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
115 }
116 out += ' } ';
117 } else {
118 out += ' {} ';
119 }
120 var __err = out;
121 out = $$outStack.pop();
122 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
123 if (it.async) {
124 out += ' throw new ValidationError([' + (__err) + ']); ';
125 } else {
126 out += ' validate.errors = [' + (__err) + ']; return false; ';
127 }
128 } else {
129 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
130 }
131 $errSchemaPath = $currErrSchemaPath;
132 if ($breakOnError) {
133 out += ' break; ';
134 }
135 }
136 } else if ($additionalIsSchema) {
137 if ($removeAdditional == 'failing') {
138 out += ' var ' + ($errs) + ' = errors; ';
139 var $wasComposite = it.compositeRule;
140 it.compositeRule = $it.compositeRule = true;
141 $it.schema = $aProperties;
142 $it.schemaPath = it.schemaPath + '.additionalProperties';
143 $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
144 $it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
145 var $passData = $data + '[' + $key + ']';
146 $it.dataPathArr[$dataNxt] = $key;
147 var $code = it.validate($it);
148 $it.baseId = $currentBaseId;
149 if (it.util.varOccurences($code, $nextData) < 2) {
150 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
151 } else {
152 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
153 }
154 out += ' if (!' + ($nextValid) + ') { errors = ' + ($errs) + '; if (validate.errors !== null) { if (errors) validate.errors.length = errors; else validate.errors = null; } delete ' + ($data) + '[' + ($key) + ']; } ';
155 it.compositeRule = $it.compositeRule = $wasComposite;
156 } else {
157 $it.schema = $aProperties;
158 $it.schemaPath = it.schemaPath + '.additionalProperties';
159 $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
160 $it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
161 var $passData = $data + '[' + $key + ']';
162 $it.dataPathArr[$dataNxt] = $key;
163 var $code = it.validate($it);
164 $it.baseId = $currentBaseId;
165 if (it.util.varOccurences($code, $nextData) < 2) {
166 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
167 } else {
168 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
169 }
170 if ($breakOnError) {
171 out += ' if (!' + ($nextValid) + ') break; ';
172 }
173 }
174 }
175 it.errorPath = $currentErrorPath;
176 }
177 if ($someProperties) {
178 out += ' } ';
179 }
180 out += ' } ';
181 if ($breakOnError) {
182 out += ' if (' + ($nextValid) + ') { ';
183 $closingBraces += '}';
184 }
185 }
186 var $useDefaults = it.opts.useDefaults && !it.compositeRule;
187 if ($schemaKeys.length) {
188 var arr4 = $schemaKeys;
189 if (arr4) {
190 var $propertyKey, i4 = -1,
191 l4 = arr4.length - 1;
192 while (i4 < l4) {
193 $propertyKey = arr4[i4 += 1];
194 var $sch = $schema[$propertyKey];
195 if (it.util.schemaHasRules($sch, it.RULES.all)) {
196 var $prop = it.util.getProperty($propertyKey),
197 $passData = $data + $prop,
198 $hasDefault = $useDefaults && $sch.default !== undefined;
199 $it.schema = $sch;
200 $it.schemaPath = $schemaPath + $prop;
201 $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($propertyKey);
202 $it.errorPath = it.util.getPath(it.errorPath, $propertyKey, it.opts.jsonPointers);
203 $it.dataPathArr[$dataNxt] = it.util.toQuotedString($propertyKey);
204 var $code = it.validate($it);
205 $it.baseId = $currentBaseId;
206 if (it.util.varOccurences($code, $nextData) < 2) {
207 $code = it.util.varReplace($code, $nextData, $passData);
208 var $useData = $passData;
209 } else {
210 var $useData = $nextData;
211 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ';
212 }
213 if ($hasDefault) {
214 out += ' ' + ($code) + ' ';
215 } else {
216 if ($requiredHash && $requiredHash[$propertyKey]) {
217 out += ' if ( ' + ($useData) + ' === undefined ';
218 if ($ownProperties) {
219 out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
220 }
221 out += ') { ' + ($nextValid) + ' = false; ';
222 var $currentErrorPath = it.errorPath,
223 $currErrSchemaPath = $errSchemaPath,
224 $missingProperty = it.util.escapeQuotes($propertyKey);
225 if (it.opts._errorDataPathProperty) {
226 it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers);
227 }
228 $errSchemaPath = it.errSchemaPath + '/required';
229 var $$outStack = $$outStack || [];
230 $$outStack.push(out);
231 out = ''; /* istanbul ignore else */
232 if (it.createErrors !== false) {
233 out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
234 if (it.opts.messages !== false) {
235 out += ' , message: \'';
236 if (it.opts._errorDataPathProperty) {
237 out += 'is a required property';
238 } else {
239 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
240 }
241 out += '\' ';
242 }
243 if (it.opts.verbose) {
244 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
245 }
246 out += ' } ';
247 } else {
248 out += ' {} ';
249 }
250 var __err = out;
251 out = $$outStack.pop();
252 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
253 if (it.async) {
254 out += ' throw new ValidationError([' + (__err) + ']); ';
255 } else {
256 out += ' validate.errors = [' + (__err) + ']; return false; ';
257 }
258 } else {
259 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
260 }
261 $errSchemaPath = $currErrSchemaPath;
262 it.errorPath = $currentErrorPath;
263 out += ' } else { ';
264 } else {
265 if ($breakOnError) {
266 out += ' if ( ' + ($useData) + ' === undefined ';
267 if ($ownProperties) {
268 out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
269 }
270 out += ') { ' + ($nextValid) + ' = true; } else { ';
271 } else {
272 out += ' if (' + ($useData) + ' !== undefined ';
273 if ($ownProperties) {
274 out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
275 }
276 out += ' ) { ';
277 }
278 }
279 out += ' ' + ($code) + ' } ';
280 }
281 }
282 if ($breakOnError) {
283 out += ' if (' + ($nextValid) + ') { ';
284 $closingBraces += '}';
285 }
286 }
287 }
288 }
289 if ($pPropertyKeys.length) {
290 var arr5 = $pPropertyKeys;
291 if (arr5) {
292 var $pProperty, i5 = -1,
293 l5 = arr5.length - 1;
294 while (i5 < l5) {
295 $pProperty = arr5[i5 += 1];
296 var $sch = $pProperties[$pProperty];
297 if (it.util.schemaHasRules($sch, it.RULES.all)) {
298 $it.schema = $sch;
299 $it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty);
300 $it.errSchemaPath = it.errSchemaPath + '/patternProperties/' + it.util.escapeFragment($pProperty);
301 if ($ownProperties) {
302 out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
303 } else {
304 out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
305 }
306 out += ' if (' + (it.usePattern($pProperty)) + '.test(' + ($key) + ')) { ';
307 $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
308 var $passData = $data + '[' + $key + ']';
309 $it.dataPathArr[$dataNxt] = $key;
310 var $code = it.validate($it);
311 $it.baseId = $currentBaseId;
312 if (it.util.varOccurences($code, $nextData) < 2) {
313 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
314 } else {
315 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
316 }
317 if ($breakOnError) {
318 out += ' if (!' + ($nextValid) + ') break; ';
319 }
320 out += ' } ';
321 if ($breakOnError) {
322 out += ' else ' + ($nextValid) + ' = true; ';
323 }
324 out += ' } ';
325 if ($breakOnError) {
326 out += ' if (' + ($nextValid) + ') { ';
327 $closingBraces += '}';
328 }
329 }
330 }
331 }
332 }
333 if (it.opts.patternGroups && $pgPropertyKeys.length) {
334 var arr6 = $pgPropertyKeys;
335 if (arr6) {
336 var $pgProperty, i6 = -1,
337 l6 = arr6.length - 1;
338 while (i6 < l6) {
339 $pgProperty = arr6[i6 += 1];
340 var $pgSchema = $pgProperties[$pgProperty],
341 $sch = $pgSchema.schema;
342 if (it.util.schemaHasRules($sch, it.RULES.all)) {
343 $it.schema = $sch;
344 $it.schemaPath = it.schemaPath + '.patternGroups' + it.util.getProperty($pgProperty) + '.schema';
345 $it.errSchemaPath = it.errSchemaPath + '/patternGroups/' + it.util.escapeFragment($pgProperty) + '/schema';
346 out += ' var pgPropCount' + ($lvl) + ' = 0; ';
347 if ($ownProperties) {
348 out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
349 } else {
350 out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
351 }
352 out += ' if (' + (it.usePattern($pgProperty)) + '.test(' + ($key) + ')) { pgPropCount' + ($lvl) + '++; ';
353 $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
354 var $passData = $data + '[' + $key + ']';
355 $it.dataPathArr[$dataNxt] = $key;
356 var $code = it.validate($it);
357 $it.baseId = $currentBaseId;
358 if (it.util.varOccurences($code, $nextData) < 2) {
359 out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
360 } else {
361 out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
362 }
363 if ($breakOnError) {
364 out += ' if (!' + ($nextValid) + ') break; ';
365 }
366 out += ' } ';
367 if ($breakOnError) {
368 out += ' else ' + ($nextValid) + ' = true; ';
369 }
370 out += ' } ';
371 if ($breakOnError) {
372 out += ' if (' + ($nextValid) + ') { ';
373 $closingBraces += '}';
374 }
375 var $pgMin = $pgSchema.minimum,
376 $pgMax = $pgSchema.maximum;
377 if ($pgMin !== undefined || $pgMax !== undefined) {
378 out += ' var ' + ($valid) + ' = true; ';
379 var $currErrSchemaPath = $errSchemaPath;
380 if ($pgMin !== undefined) {
381 var $limit = $pgMin,
382 $reason = 'minimum',
383 $moreOrLess = 'less';
384 out += ' ' + ($valid) + ' = pgPropCount' + ($lvl) + ' >= ' + ($pgMin) + '; ';
385 $errSchemaPath = it.errSchemaPath + '/patternGroups/minimum';
386 out += ' if (!' + ($valid) + ') { ';
387 var $$outStack = $$outStack || [];
388 $$outStack.push(out);
389 out = ''; /* istanbul ignore else */
390 if (it.createErrors !== false) {
391 out += ' { keyword: \'' + ('patternGroups') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { reason: \'' + ($reason) + '\', limit: ' + ($limit) + ', pattern: \'' + (it.util.escapeQuotes($pgProperty)) + '\' } ';
392 if (it.opts.messages !== false) {
393 out += ' , message: \'should NOT have ' + ($moreOrLess) + ' than ' + ($limit) + ' properties matching pattern "' + (it.util.escapeQuotes($pgProperty)) + '"\' ';
394 }
395 if (it.opts.verbose) {
396 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
397 }
398 out += ' } ';
399 } else {
400 out += ' {} ';
401 }
402 var __err = out;
403 out = $$outStack.pop();
404 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
405 if (it.async) {
406 out += ' throw new ValidationError([' + (__err) + ']); ';
407 } else {
408 out += ' validate.errors = [' + (__err) + ']; return false; ';
409 }
410 } else {
411 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
412 }
413 out += ' } ';
414 if ($pgMax !== undefined) {
415 out += ' else ';
416 }
417 }
418 if ($pgMax !== undefined) {
419 var $limit = $pgMax,
420 $reason = 'maximum',
421 $moreOrLess = 'more';
422 out += ' ' + ($valid) + ' = pgPropCount' + ($lvl) + ' <= ' + ($pgMax) + '; ';
423 $errSchemaPath = it.errSchemaPath + '/patternGroups/maximum';
424 out += ' if (!' + ($valid) + ') { ';
425 var $$outStack = $$outStack || [];
426 $$outStack.push(out);
427 out = ''; /* istanbul ignore else */
428 if (it.createErrors !== false) {
429 out += ' { keyword: \'' + ('patternGroups') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { reason: \'' + ($reason) + '\', limit: ' + ($limit) + ', pattern: \'' + (it.util.escapeQuotes($pgProperty)) + '\' } ';
430 if (it.opts.messages !== false) {
431 out += ' , message: \'should NOT have ' + ($moreOrLess) + ' than ' + ($limit) + ' properties matching pattern "' + (it.util.escapeQuotes($pgProperty)) + '"\' ';
432 }
433 if (it.opts.verbose) {
434 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
435 }
436 out += ' } ';
437 } else {
438 out += ' {} ';
439 }
440 var __err = out;
441 out = $$outStack.pop();
442 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
443 if (it.async) {
444 out += ' throw new ValidationError([' + (__err) + ']); ';
445 } else {
446 out += ' validate.errors = [' + (__err) + ']; return false; ';
447 }
448 } else {
449 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
450 }
451 out += ' } ';
452 }
453 $errSchemaPath = $currErrSchemaPath;
454 if ($breakOnError) {
455 out += ' if (' + ($valid) + ') { ';
456 $closingBraces += '}';
457 }
458 }
459 }
460 }
461 }
462 }
463 if ($breakOnError) {
464 out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
465 }
466 out = it.util.cleanUpCode(out);
467 return out;
468}