Initial commit
[yaffs-website] / node_modules / ajv / lib / dotjs / required.js
1 'use strict';
2 module.exports = function generate_required(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 $isData = it.opts.v5 && $schema && $schema.$data,
14     $schemaValue;
15   if ($isData) {
16     out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
17     $schemaValue = 'schema' + $lvl;
18   } else {
19     $schemaValue = $schema;
20   }
21   var $vSchema = 'schema' + $lvl;
22   if (!$isData) {
23     if ($schema.length < it.opts.loopRequired && it.schema.properties && Object.keys(it.schema.properties).length) {
24       var $required = [];
25       var arr1 = $schema;
26       if (arr1) {
27         var $property, i1 = -1,
28           l1 = arr1.length - 1;
29         while (i1 < l1) {
30           $property = arr1[i1 += 1];
31           var $propertySch = it.schema.properties[$property];
32           if (!($propertySch && it.util.schemaHasRules($propertySch, it.RULES.all))) {
33             $required[$required.length] = $property;
34           }
35         }
36       }
37     } else {
38       var $required = $schema;
39     }
40   }
41   if ($isData || $required.length) {
42     var $currentErrorPath = it.errorPath,
43       $loopRequired = $isData || $required.length >= it.opts.loopRequired;
44     if ($breakOnError) {
45       out += ' var missing' + ($lvl) + '; ';
46       if ($loopRequired) {
47         if (!$isData) {
48           out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + '; ';
49         }
50         var $i = 'i' + $lvl,
51           $propertyPath = 'schema' + $lvl + '[' + $i + ']',
52           $missingProperty = '\' + ' + $propertyPath + ' + \'';
53         if (it.opts._errorDataPathProperty) {
54           it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);
55         }
56         out += ' var ' + ($valid) + ' = true; ';
57         if ($isData) {
58           out += ' if (schema' + ($lvl) + ' === undefined) ' + ($valid) + ' = true; else if (!Array.isArray(schema' + ($lvl) + ')) ' + ($valid) + ' = false; else {';
59         }
60         out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { ' + ($valid) + ' = ' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] !== undefined; if (!' + ($valid) + ') break; } ';
61         if ($isData) {
62           out += '  }  ';
63         }
64         out += '  if (!' + ($valid) + ') {   ';
65         var $$outStack = $$outStack || [];
66         $$outStack.push(out);
67         out = ''; /* istanbul ignore else */
68         if (it.createErrors !== false) {
69           out += ' { keyword: \'' + ($errorKeyword || 'required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
70           if (it.opts.messages !== false) {
71             out += ' , message: \'';
72             if (it.opts._errorDataPathProperty) {
73               out += 'is a required property';
74             } else {
75               out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
76             }
77             out += '\' ';
78           }
79           if (it.opts.verbose) {
80             out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
81           }
82           out += ' } ';
83         } else {
84           out += ' {} ';
85         }
86         var __err = out;
87         out = $$outStack.pop();
88         if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
89           if (it.async) {
90             out += ' throw new ValidationError([' + (__err) + ']); ';
91           } else {
92             out += ' validate.errors = [' + (__err) + ']; return false; ';
93           }
94         } else {
95           out += ' var err = ' + (__err) + ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
96         }
97         out += ' } else { ';
98       } else {
99         out += ' if ( ';
100         var arr2 = $required;
101         if (arr2) {
102           var _$property, $i = -1,
103             l2 = arr2.length - 1;
104           while ($i < l2) {
105             _$property = arr2[$i += 1];
106             if ($i) {
107               out += ' || ';
108             }
109             var $prop = it.util.getProperty(_$property);
110             out += ' ( ' + ($data) + ($prop) + ' === undefined && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? _$property : $prop)) + ') ) ';
111           }
112         }
113         out += ') {  ';
114         var $propertyPath = 'missing' + $lvl,
115           $missingProperty = '\' + ' + $propertyPath + ' + \'';
116         if (it.opts._errorDataPathProperty) {
117           it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath;
118         }
119         var $$outStack = $$outStack || [];
120         $$outStack.push(out);
121         out = ''; /* istanbul ignore else */
122         if (it.createErrors !== false) {
123           out += ' { keyword: \'' + ($errorKeyword || 'required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
124           if (it.opts.messages !== false) {
125             out += ' , message: \'';
126             if (it.opts._errorDataPathProperty) {
127               out += 'is a required property';
128             } else {
129               out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
130             }
131             out += '\' ';
132           }
133           if (it.opts.verbose) {
134             out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
135           }
136           out += ' } ';
137         } else {
138           out += ' {} ';
139         }
140         var __err = out;
141         out = $$outStack.pop();
142         if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
143           if (it.async) {
144             out += ' throw new ValidationError([' + (__err) + ']); ';
145           } else {
146             out += ' validate.errors = [' + (__err) + ']; return false; ';
147           }
148         } else {
149           out += ' var err = ' + (__err) + ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
150         }
151         out += ' } else { ';
152       }
153     } else {
154       if ($loopRequired) {
155         if (!$isData) {
156           out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + '; ';
157         }
158         var $i = 'i' + $lvl,
159           $propertyPath = 'schema' + $lvl + '[' + $i + ']',
160           $missingProperty = '\' + ' + $propertyPath + ' + \'';
161         if (it.opts._errorDataPathProperty) {
162           it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);
163         }
164         if ($isData) {
165           out += ' if (' + ($vSchema) + ' && !Array.isArray(' + ($vSchema) + ')) {  var err =   '; /* istanbul ignore else */
166           if (it.createErrors !== false) {
167             out += ' { keyword: \'' + ($errorKeyword || 'required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
168             if (it.opts.messages !== false) {
169               out += ' , message: \'';
170               if (it.opts._errorDataPathProperty) {
171                 out += 'is a required property';
172               } else {
173                 out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
174               }
175               out += '\' ';
176             }
177             if (it.opts.verbose) {
178               out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
179             }
180             out += ' } ';
181           } else {
182             out += ' {} ';
183           }
184           out += ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else if (' + ($vSchema) + ' !== undefined) { ';
185         }
186         out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { if (' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] === undefined) {  var err =   '; /* istanbul ignore else */
187         if (it.createErrors !== false) {
188           out += ' { keyword: \'' + ($errorKeyword || 'required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
189           if (it.opts.messages !== false) {
190             out += ' , message: \'';
191             if (it.opts._errorDataPathProperty) {
192               out += 'is a required property';
193             } else {
194               out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
195             }
196             out += '\' ';
197           }
198           if (it.opts.verbose) {
199             out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
200           }
201           out += ' } ';
202         } else {
203           out += ' {} ';
204         }
205         out += ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } } ';
206         if ($isData) {
207           out += '  }  ';
208         }
209       } else {
210         var arr3 = $required;
211         if (arr3) {
212           var $reqProperty, i3 = -1,
213             l3 = arr3.length - 1;
214           while (i3 < l3) {
215             $reqProperty = arr3[i3 += 1];
216             var $prop = it.util.getProperty($reqProperty),
217               $missingProperty = it.util.escapeQuotes($reqProperty);
218             if (it.opts._errorDataPathProperty) {
219               it.errorPath = it.util.getPath($currentErrorPath, $reqProperty, it.opts.jsonPointers);
220             }
221             out += ' if (' + ($data) + ($prop) + ' === undefined) {  var err =   '; /* istanbul ignore else */
222             if (it.createErrors !== false) {
223               out += ' { keyword: \'' + ($errorKeyword || 'required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
224               if (it.opts.messages !== false) {
225                 out += ' , message: \'';
226                 if (it.opts._errorDataPathProperty) {
227                   out += 'is a required property';
228                 } else {
229                   out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
230                 }
231                 out += '\' ';
232               }
233               if (it.opts.verbose) {
234                 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
235               }
236               out += ' } ';
237             } else {
238               out += ' {} ';
239             }
240             out += ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';
241           }
242         }
243       }
244     }
245     it.errorPath = $currentErrorPath;
246   } else if ($breakOnError) {
247     out += ' if (true) {';
248   }
249   return out;
250 }