Updated from some -dev modules to alpha, beta or full releases
[yaffs-website] / node_modules / ajv / lib / dotjs / validate.js
1 'use strict';
2 module.exports = function generate_validate(it, $keyword) {
3   var out = '';
4   var $async = it.schema.$async === true;
5   if (it.isTop) {
6     var $top = it.isTop,
7       $lvl = it.level = 0,
8       $dataLvl = it.dataLevel = 0,
9       $data = 'data';
10     it.rootId = it.resolve.fullPath(it.root.schema.id);
11     it.baseId = it.baseId || it.rootId;
12     if ($async) {
13       it.async = true;
14       var $es7 = it.opts.async == 'es7';
15       it.yieldAwait = $es7 ? 'await' : 'yield';
16     }
17     delete it.isTop;
18     it.dataPathArr = [undefined];
19     out += ' var validate = ';
20     if ($async) {
21       if ($es7) {
22         out += ' (async function ';
23       } else {
24         if (it.opts.async == 'co*') {
25           out += 'co.wrap';
26         }
27         out += '(function* ';
28       }
29     } else {
30       out += ' (function ';
31     }
32     out += ' (data, dataPath, parentData, parentDataProperty, rootData) { \'use strict\'; var vErrors = null; ';
33     out += ' var errors = 0;     ';
34     out += ' if (rootData === undefined) rootData = data;';
35   } else {
36     var $lvl = it.level,
37       $dataLvl = it.dataLevel,
38       $data = 'data' + ($dataLvl || '');
39     if (it.schema.id) it.baseId = it.resolve.url(it.baseId, it.schema.id);
40     if ($async && !it.async) throw new Error('async schema in sync schema');
41     out += ' var errs_' + ($lvl) + ' = errors;';
42   }
43   var $valid = 'valid' + $lvl,
44     $breakOnError = !it.opts.allErrors,
45     $closingBraces1 = '',
46     $closingBraces2 = '',
47     $errorKeyword;
48   var $typeSchema = it.schema.type,
49     $typeIsArray = Array.isArray($typeSchema);
50   if ($typeSchema && it.opts.coerceTypes) {
51     var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema);
52     if ($coerceToTypes) {
53       var $schemaPath = it.schemaPath + '.type',
54         $errSchemaPath = it.errSchemaPath + '/type',
55         $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
56       out += ' if (' + (it.util[$method]($typeSchema, $data, true)) + ') {  ';
57       var $dataType = 'dataType' + $lvl,
58         $coerced = 'coerced' + $lvl;
59       out += ' var ' + ($dataType) + ' = typeof ' + ($data) + '; ';
60       if (it.opts.coerceTypes == 'array') {
61         out += ' if (' + ($dataType) + ' == \'object\' && Array.isArray(' + ($data) + ')) ' + ($dataType) + ' = \'array\'; ';
62       }
63       out += ' var ' + ($coerced) + ' = undefined; ';
64       var $bracesCoercion = '';
65       var arr1 = $coerceToTypes;
66       if (arr1) {
67         var $type, $i = -1,
68           l1 = arr1.length - 1;
69         while ($i < l1) {
70           $type = arr1[$i += 1];
71           if ($i) {
72             out += ' if (' + ($coerced) + ' === undefined) { ';
73             $bracesCoercion += '}';
74           }
75           if (it.opts.coerceTypes == 'array' && $type != 'array') {
76             out += ' if (' + ($dataType) + ' == \'array\' && ' + ($data) + '.length == 1) { ' + ($coerced) + ' = ' + ($data) + ' = ' + ($data) + '[0]; ' + ($dataType) + ' = typeof ' + ($data) + ';  } ';
77           }
78           if ($type == 'string') {
79             out += ' if (' + ($dataType) + ' == \'number\' || ' + ($dataType) + ' == \'boolean\') ' + ($coerced) + ' = \'\' + ' + ($data) + '; else if (' + ($data) + ' === null) ' + ($coerced) + ' = \'\'; ';
80           } else if ($type == 'number' || $type == 'integer') {
81             out += ' if (' + ($dataType) + ' == \'boolean\' || ' + ($data) + ' === null || (' + ($dataType) + ' == \'string\' && ' + ($data) + ' && ' + ($data) + ' == +' + ($data) + ' ';
82             if ($type == 'integer') {
83               out += ' && !(' + ($data) + ' % 1)';
84             }
85             out += ')) ' + ($coerced) + ' = +' + ($data) + '; ';
86           } else if ($type == 'boolean') {
87             out += ' if (' + ($data) + ' === \'false\' || ' + ($data) + ' === 0 || ' + ($data) + ' === null) ' + ($coerced) + ' = false; else if (' + ($data) + ' === \'true\' || ' + ($data) + ' === 1) ' + ($coerced) + ' = true; ';
88           } else if ($type == 'null') {
89             out += ' if (' + ($data) + ' === \'\' || ' + ($data) + ' === 0 || ' + ($data) + ' === false) ' + ($coerced) + ' = null; ';
90           } else if (it.opts.coerceTypes == 'array' && $type == 'array') {
91             out += ' if (' + ($dataType) + ' == \'string\' || ' + ($dataType) + ' == \'number\' || ' + ($dataType) + ' == \'boolean\' || ' + ($data) + ' == null) ' + ($coerced) + ' = [' + ($data) + ']; ';
92           }
93         }
94       }
95       out += ' ' + ($bracesCoercion) + ' if (' + ($coerced) + ' === undefined) {   ';
96       var $$outStack = $$outStack || [];
97       $$outStack.push(out);
98       out = ''; /* istanbul ignore else */
99       if (it.createErrors !== false) {
100         out += ' { keyword: \'' + ($errorKeyword || 'type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
101         if ($typeIsArray) {
102           out += '' + ($typeSchema.join(","));
103         } else {
104           out += '' + ($typeSchema);
105         }
106         out += '\' } ';
107         if (it.opts.messages !== false) {
108           out += ' , message: \'should be ';
109           if ($typeIsArray) {
110             out += '' + ($typeSchema.join(","));
111           } else {
112             out += '' + ($typeSchema);
113           }
114           out += '\' ';
115         }
116         if (it.opts.verbose) {
117           out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
118         }
119         out += ' } ';
120       } else {
121         out += ' {} ';
122       }
123       var __err = out;
124       out = $$outStack.pop();
125       if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
126         if (it.async) {
127           out += ' throw new ValidationError([' + (__err) + ']); ';
128         } else {
129           out += ' validate.errors = [' + (__err) + ']; return false; ';
130         }
131       } else {
132         out += ' var err = ' + (__err) + ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
133       }
134       out += ' } else {  ';
135       var $parentData = $dataLvl ? 'data' + (($dataLvl - 1) || '') : 'parentData',
136         $parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
137       out += ' ' + ($data) + ' = ' + ($coerced) + '; ';
138       if (!$dataLvl) {
139         out += 'if (' + ($parentData) + ' !== undefined)';
140       }
141       out += ' ' + ($parentData) + '[' + ($parentDataProperty) + '] = ' + ($coerced) + '; } } ';
142     }
143   }
144   var $refKeywords;
145   if (it.schema.$ref && ($refKeywords = it.util.schemaHasRulesExcept(it.schema, it.RULES.all, '$ref'))) {
146     if (it.opts.extendRefs == 'fail') {
147       throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '"');
148     } else if (it.opts.extendRefs == 'ignore') {
149       $refKeywords = false;
150       console.log('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
151     } else if (it.opts.extendRefs !== true) {
152       console.log('$ref: all keywords used in schema at path "' + it.errSchemaPath + '". It will change in the next major version, see issue #260. Use option { extendRefs: true } to keep current behaviour');
153     }
154   }
155   if (it.schema.$ref && !$refKeywords) {
156     out += ' ' + (it.RULES.all.$ref.code(it, '$ref')) + ' ';
157     if ($breakOnError) {
158       out += ' } if (errors === ';
159       if ($top) {
160         out += '0';
161       } else {
162         out += 'errs_' + ($lvl);
163       }
164       out += ') { ';
165       $closingBraces2 += '}';
166     }
167   } else {
168     var arr2 = it.RULES;
169     if (arr2) {
170       var $rulesGroup, i2 = -1,
171         l2 = arr2.length - 1;
172       while (i2 < l2) {
173         $rulesGroup = arr2[i2 += 1];
174         if ($shouldUseGroup($rulesGroup)) {
175           if ($rulesGroup.type) {
176             out += ' if (' + (it.util.checkDataType($rulesGroup.type, $data)) + ') { ';
177           }
178           if (it.opts.useDefaults && !it.compositeRule) {
179             if ($rulesGroup.type == 'object' && it.schema.properties) {
180               var $schema = it.schema.properties,
181                 $schemaKeys = Object.keys($schema);
182               var arr3 = $schemaKeys;
183               if (arr3) {
184                 var $propertyKey, i3 = -1,
185                   l3 = arr3.length - 1;
186                 while (i3 < l3) {
187                   $propertyKey = arr3[i3 += 1];
188                   var $sch = $schema[$propertyKey];
189                   if ($sch.default !== undefined) {
190                     var $passData = $data + it.util.getProperty($propertyKey);
191                     out += '  if (' + ($passData) + ' === undefined) ' + ($passData) + ' = ';
192                     if (it.opts.useDefaults == 'shared') {
193                       out += ' ' + (it.useDefault($sch.default)) + ' ';
194                     } else {
195                       out += ' ' + (JSON.stringify($sch.default)) + ' ';
196                     }
197                     out += '; ';
198                   }
199                 }
200               }
201             } else if ($rulesGroup.type == 'array' && Array.isArray(it.schema.items)) {
202               var arr4 = it.schema.items;
203               if (arr4) {
204                 var $sch, $i = -1,
205                   l4 = arr4.length - 1;
206                 while ($i < l4) {
207                   $sch = arr4[$i += 1];
208                   if ($sch.default !== undefined) {
209                     var $passData = $data + '[' + $i + ']';
210                     out += '  if (' + ($passData) + ' === undefined) ' + ($passData) + ' = ';
211                     if (it.opts.useDefaults == 'shared') {
212                       out += ' ' + (it.useDefault($sch.default)) + ' ';
213                     } else {
214                       out += ' ' + (JSON.stringify($sch.default)) + ' ';
215                     }
216                     out += '; ';
217                   }
218                 }
219               }
220             }
221           }
222           var arr5 = $rulesGroup.rules;
223           if (arr5) {
224             var $rule, i5 = -1,
225               l5 = arr5.length - 1;
226             while (i5 < l5) {
227               $rule = arr5[i5 += 1];
228               if ($shouldUseRule($rule)) {
229                 out += ' ' + ($rule.code(it, $rule.keyword)) + ' ';
230                 if ($breakOnError) {
231                   $closingBraces1 += '}';
232                 }
233               }
234             }
235           }
236           if ($breakOnError) {
237             out += ' ' + ($closingBraces1) + ' ';
238             $closingBraces1 = '';
239           }
240           if ($rulesGroup.type) {
241             out += ' } ';
242             if ($typeSchema && $typeSchema === $rulesGroup.type) {
243               var $typeChecked = true;
244               out += ' else { ';
245               var $schemaPath = it.schemaPath + '.type',
246                 $errSchemaPath = it.errSchemaPath + '/type';
247               var $$outStack = $$outStack || [];
248               $$outStack.push(out);
249               out = ''; /* istanbul ignore else */
250               if (it.createErrors !== false) {
251                 out += ' { keyword: \'' + ($errorKeyword || 'type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
252                 if ($typeIsArray) {
253                   out += '' + ($typeSchema.join(","));
254                 } else {
255                   out += '' + ($typeSchema);
256                 }
257                 out += '\' } ';
258                 if (it.opts.messages !== false) {
259                   out += ' , message: \'should be ';
260                   if ($typeIsArray) {
261                     out += '' + ($typeSchema.join(","));
262                   } else {
263                     out += '' + ($typeSchema);
264                   }
265                   out += '\' ';
266                 }
267                 if (it.opts.verbose) {
268                   out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
269                 }
270                 out += ' } ';
271               } else {
272                 out += ' {} ';
273               }
274               var __err = out;
275               out = $$outStack.pop();
276               if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
277                 if (it.async) {
278                   out += ' throw new ValidationError([' + (__err) + ']); ';
279                 } else {
280                   out += ' validate.errors = [' + (__err) + ']; return false; ';
281                 }
282               } else {
283                 out += ' var err = ' + (__err) + ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
284               }
285               out += ' } ';
286             }
287           }
288           if ($breakOnError) {
289             out += ' if (errors === ';
290             if ($top) {
291               out += '0';
292             } else {
293               out += 'errs_' + ($lvl);
294             }
295             out += ') { ';
296             $closingBraces2 += '}';
297           }
298         }
299       }
300     }
301   }
302   if ($typeSchema && !$typeChecked && !(it.opts.coerceTypes && $coerceToTypes)) {
303     var $schemaPath = it.schemaPath + '.type',
304       $errSchemaPath = it.errSchemaPath + '/type',
305       $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
306     out += ' if (' + (it.util[$method]($typeSchema, $data, true)) + ') {   ';
307     var $$outStack = $$outStack || [];
308     $$outStack.push(out);
309     out = ''; /* istanbul ignore else */
310     if (it.createErrors !== false) {
311       out += ' { keyword: \'' + ($errorKeyword || 'type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
312       if ($typeIsArray) {
313         out += '' + ($typeSchema.join(","));
314       } else {
315         out += '' + ($typeSchema);
316       }
317       out += '\' } ';
318       if (it.opts.messages !== false) {
319         out += ' , message: \'should be ';
320         if ($typeIsArray) {
321           out += '' + ($typeSchema.join(","));
322         } else {
323           out += '' + ($typeSchema);
324         }
325         out += '\' ';
326       }
327       if (it.opts.verbose) {
328         out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
329       }
330       out += ' } ';
331     } else {
332       out += ' {} ';
333     }
334     var __err = out;
335     out = $$outStack.pop();
336     if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
337       if (it.async) {
338         out += ' throw new ValidationError([' + (__err) + ']); ';
339       } else {
340         out += ' validate.errors = [' + (__err) + ']; return false; ';
341       }
342     } else {
343       out += ' var err = ' + (__err) + ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
344     }
345     out += ' }';
346   }
347   if ($breakOnError) {
348     out += ' ' + ($closingBraces2) + ' ';
349   }
350   if ($top) {
351     if ($async) {
352       out += ' if (errors === 0) return true;           ';
353       out += ' else throw new ValidationError(vErrors); ';
354     } else {
355       out += ' validate.errors = vErrors; ';
356       out += ' return errors === 0;       ';
357     }
358     out += ' }); return validate;';
359   } else {
360     out += ' var ' + ($valid) + ' = errors === errs_' + ($lvl) + ';';
361   }
362   out = it.util.cleanUpCode(out);
363   if ($top && $breakOnError) {
364     out = it.util.cleanUpVarErrors(out, $async);
365   }
366
367   function $shouldUseGroup($rulesGroup) {
368     for (var i = 0; i < $rulesGroup.rules.length; i++)
369       if ($shouldUseRule($rulesGroup.rules[i])) return true;
370   }
371
372   function $shouldUseRule($rule) {
373     return it.schema[$rule.keyword] !== undefined || ($rule.keyword == 'properties' && (it.schema.additionalProperties === false || typeof it.schema.additionalProperties == 'object' || (it.schema.patternProperties && Object.keys(it.schema.patternProperties).length) || (it.opts.v5 && it.schema.patternGroups && Object.keys(it.schema.patternGroups).length)));
374   }
375   return out;
376 }