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