Initial commit
[yaffs-website] / node_modules / ajv / lib / dotjs / switch.js
1 'use strict';
2 module.exports = function generate_switch(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 $ifPassed = 'ifPassed' + it.level,
19     $currentBaseId = $it.baseId,
20     $shouldContinue;
21   out += 'var ' + ($ifPassed) + ';';
22   var arr1 = $schema;
23   if (arr1) {
24     var $sch, $caseIndex = -1,
25       l1 = arr1.length - 1;
26     while ($caseIndex < l1) {
27       $sch = arr1[$caseIndex += 1];
28       if ($caseIndex && !$shouldContinue) {
29         out += ' if (!' + ($ifPassed) + ') { ';
30         $closingBraces += '}';
31       }
32       if ($sch.if && it.util.schemaHasRules($sch.if, it.RULES.all)) {
33         out += ' var ' + ($errs) + ' = errors;   ';
34         var $wasComposite = it.compositeRule;
35         it.compositeRule = $it.compositeRule = true;
36         $it.createErrors = false;
37         $it.schema = $sch.if;
38         $it.schemaPath = $schemaPath + '[' + $caseIndex + '].if';
39         $it.errSchemaPath = $errSchemaPath + '/' + $caseIndex + '/if';
40         out += '  ' + (it.validate($it)) + ' ';
41         $it.baseId = $currentBaseId;
42         $it.createErrors = true;
43         it.compositeRule = $it.compositeRule = $wasComposite;
44         out += ' ' + ($ifPassed) + ' = ' + ($nextValid) + '; if (' + ($ifPassed) + ') {  ';
45         if (typeof $sch.then == 'boolean') {
46           if ($sch.then === false) {
47             var $$outStack = $$outStack || [];
48             $$outStack.push(out);
49             out = ''; /* istanbul ignore else */
50             if (it.createErrors !== false) {
51               out += ' { keyword: \'' + ($errorKeyword || 'switch') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { caseIndex: ' + ($caseIndex) + ' } ';
52               if (it.opts.messages !== false) {
53                 out += ' , message: \'should pass "switch" keyword validation\' ';
54               }
55               if (it.opts.verbose) {
56                 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
57               }
58               out += ' } ';
59             } else {
60               out += ' {} ';
61             }
62             var __err = out;
63             out = $$outStack.pop();
64             if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
65               if (it.async) {
66                 out += ' throw new ValidationError([' + (__err) + ']); ';
67               } else {
68                 out += ' validate.errors = [' + (__err) + ']; return false; ';
69               }
70             } else {
71               out += ' var err = ' + (__err) + ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
72             }
73           }
74           out += ' var ' + ($nextValid) + ' = ' + ($sch.then) + '; ';
75         } else {
76           $it.schema = $sch.then;
77           $it.schemaPath = $schemaPath + '[' + $caseIndex + '].then';
78           $it.errSchemaPath = $errSchemaPath + '/' + $caseIndex + '/then';
79           out += '  ' + (it.validate($it)) + ' ';
80           $it.baseId = $currentBaseId;
81         }
82         out += '  } else {  errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } } ';
83       } else {
84         out += ' ' + ($ifPassed) + ' = true;  ';
85         if (typeof $sch.then == 'boolean') {
86           if ($sch.then === false) {
87             var $$outStack = $$outStack || [];
88             $$outStack.push(out);
89             out = ''; /* istanbul ignore else */
90             if (it.createErrors !== false) {
91               out += ' { keyword: \'' + ($errorKeyword || 'switch') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { caseIndex: ' + ($caseIndex) + ' } ';
92               if (it.opts.messages !== false) {
93                 out += ' , message: \'should pass "switch" keyword validation\' ';
94               }
95               if (it.opts.verbose) {
96                 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
97               }
98               out += ' } ';
99             } else {
100               out += ' {} ';
101             }
102             var __err = out;
103             out = $$outStack.pop();
104             if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
105               if (it.async) {
106                 out += ' throw new ValidationError([' + (__err) + ']); ';
107               } else {
108                 out += ' validate.errors = [' + (__err) + ']; return false; ';
109               }
110             } else {
111               out += ' var err = ' + (__err) + ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
112             }
113           }
114           out += ' var ' + ($nextValid) + ' = ' + ($sch.then) + '; ';
115         } else {
116           $it.schema = $sch.then;
117           $it.schemaPath = $schemaPath + '[' + $caseIndex + '].then';
118           $it.errSchemaPath = $errSchemaPath + '/' + $caseIndex + '/then';
119           out += '  ' + (it.validate($it)) + ' ';
120           $it.baseId = $currentBaseId;
121         }
122       }
123       $shouldContinue = $sch.continue
124     }
125   }
126   out += '' + ($closingBraces) + 'var ' + ($valid) + ' = ' + ($nextValid) + '; ';
127   out = it.util.cleanUpCode(out);
128   return out;
129 }