Initial commit
[yaffs-website] / node_modules / ajv / lib / dotjs / items.js
1 'use strict';
2 module.exports = function generate_items(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 $idx = 'i' + $lvl,
19     $dataNxt = $it.dataLevel = it.dataLevel + 1,
20     $nextData = 'data' + $dataNxt,
21     $currentBaseId = it.baseId;
22   out += 'var ' + ($errs) + ' = errors;var ' + ($valid) + ';';
23   if (Array.isArray($schema)) {
24     var $additionalItems = it.schema.additionalItems;
25     if ($additionalItems === false) {
26       out += ' ' + ($valid) + ' = ' + ($data) + '.length <= ' + ($schema.length) + '; ';
27       var $currErrSchemaPath = $errSchemaPath;
28       $errSchemaPath = it.errSchemaPath + '/additionalItems';
29       out += '  if (!' + ($valid) + ') {   ';
30       var $$outStack = $$outStack || [];
31       $$outStack.push(out);
32       out = ''; /* istanbul ignore else */
33       if (it.createErrors !== false) {
34         out += ' { keyword: \'' + ($errorKeyword || 'additionalItems') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { limit: ' + ($schema.length) + ' } ';
35         if (it.opts.messages !== false) {
36           out += ' , message: \'should NOT have more than ' + ($schema.length) + ' items\' ';
37         }
38         if (it.opts.verbose) {
39           out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
40         }
41         out += ' } ';
42       } else {
43         out += ' {} ';
44       }
45       var __err = out;
46       out = $$outStack.pop();
47       if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
48         if (it.async) {
49           out += ' throw new ValidationError([' + (__err) + ']); ';
50         } else {
51           out += ' validate.errors = [' + (__err) + ']; return false; ';
52         }
53       } else {
54         out += ' var err = ' + (__err) + ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
55       }
56       out += ' } ';
57       $errSchemaPath = $currErrSchemaPath;
58       if ($breakOnError) {
59         $closingBraces += '}';
60         out += ' else { ';
61       }
62     }
63     var arr1 = $schema;
64     if (arr1) {
65       var $sch, $i = -1,
66         l1 = arr1.length - 1;
67       while ($i < l1) {
68         $sch = arr1[$i += 1];
69         if (it.util.schemaHasRules($sch, it.RULES.all)) {
70           out += ' ' + ($nextValid) + ' = true; if (' + ($data) + '.length > ' + ($i) + ') { ';
71           var $passData = $data + '[' + $i + ']';
72           $it.schema = $sch;
73           $it.schemaPath = $schemaPath + '[' + $i + ']';
74           $it.errSchemaPath = $errSchemaPath + '/' + $i;
75           $it.errorPath = it.util.getPathExpr(it.errorPath, $i, it.opts.jsonPointers, true);
76           $it.dataPathArr[$dataNxt] = $i;
77           var $code = it.validate($it);
78           $it.baseId = $currentBaseId;
79           if (it.util.varOccurences($code, $nextData) < 2) {
80             out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
81           } else {
82             out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
83           }
84           out += ' }  ';
85           if ($breakOnError) {
86             out += ' if (' + ($nextValid) + ') { ';
87             $closingBraces += '}';
88           }
89         }
90       }
91     }
92     if (typeof $additionalItems == 'object' && it.util.schemaHasRules($additionalItems, it.RULES.all)) {
93       $it.schema = $additionalItems;
94       $it.schemaPath = it.schemaPath + '.additionalItems';
95       $it.errSchemaPath = it.errSchemaPath + '/additionalItems';
96       out += ' ' + ($nextValid) + ' = true; if (' + ($data) + '.length > ' + ($schema.length) + ') {  for (var ' + ($idx) + ' = ' + ($schema.length) + '; ' + ($idx) + ' < ' + ($data) + '.length; ' + ($idx) + '++) { ';
97       $it.errorPath = it.util.getPathExpr(it.errorPath, $idx, it.opts.jsonPointers, true);
98       var $passData = $data + '[' + $idx + ']';
99       $it.dataPathArr[$dataNxt] = $idx;
100       var $code = it.validate($it);
101       $it.baseId = $currentBaseId;
102       if (it.util.varOccurences($code, $nextData) < 2) {
103         out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
104       } else {
105         out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
106       }
107       if ($breakOnError) {
108         out += ' if (!' + ($nextValid) + ') break; ';
109       }
110       out += ' } }  ';
111       if ($breakOnError) {
112         out += ' if (' + ($nextValid) + ') { ';
113         $closingBraces += '}';
114       }
115     }
116   } else if (it.util.schemaHasRules($schema, it.RULES.all)) {
117     $it.schema = $schema;
118     $it.schemaPath = $schemaPath;
119     $it.errSchemaPath = $errSchemaPath;
120     out += '  for (var ' + ($idx) + ' = ' + (0) + '; ' + ($idx) + ' < ' + ($data) + '.length; ' + ($idx) + '++) { ';
121     $it.errorPath = it.util.getPathExpr(it.errorPath, $idx, it.opts.jsonPointers, true);
122     var $passData = $data + '[' + $idx + ']';
123     $it.dataPathArr[$dataNxt] = $idx;
124     var $code = it.validate($it);
125     $it.baseId = $currentBaseId;
126     if (it.util.varOccurences($code, $nextData) < 2) {
127       out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
128     } else {
129       out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
130     }
131     if ($breakOnError) {
132       out += ' if (!' + ($nextValid) + ') break; ';
133     }
134     out += ' }  ';
135     if ($breakOnError) {
136       out += ' if (' + ($nextValid) + ') { ';
137       $closingBraces += '}';
138     }
139   }
140   if ($breakOnError) {
141     out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
142   }
143   out = it.util.cleanUpCode(out);
144   return out;
145 }