2 module.exports = function generate_switch(it, $keyword) {
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;
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 = '';
17 var $nextValid = 'valid' + $it.level;
18 var $ifPassed = 'ifPassed' + it.level,
19 $currentBaseId = $it.baseId,
21 out += 'var ' + ($ifPassed) + ';';
24 var $sch, $caseIndex = -1,
26 while ($caseIndex < l1) {
27 $sch = arr1[$caseIndex += 1];
28 if ($caseIndex && !$shouldContinue) {
29 out += ' if (!' + ($ifPassed) + ') { ';
30 $closingBraces += '}';
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;
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 || [];
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\' ';
55 if (it.opts.verbose) {
56 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
63 out = $$outStack.pop();
64 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
66 out += ' throw new ValidationError([' + (__err) + ']); ';
68 out += ' validate.errors = [' + (__err) + ']; return false; ';
71 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
74 out += ' var ' + ($nextValid) + ' = ' + ($sch.then) + '; ';
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;
82 out += ' } else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } } ';
84 out += ' ' + ($ifPassed) + ' = true; ';
85 if (typeof $sch.then == 'boolean') {
86 if ($sch.then === false) {
87 var $$outStack = $$outStack || [];
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\' ';
95 if (it.opts.verbose) {
96 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
103 out = $$outStack.pop();
104 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
106 out += ' throw new ValidationError([' + (__err) + ']); ';
108 out += ' validate.errors = [' + (__err) + ']; return false; ';
111 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
114 out += ' var ' + ($nextValid) + ' = ' + ($sch.then) + '; ';
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;
123 $shouldContinue = $sch.continue
126 out += '' + ($closingBraces) + 'var ' + ($valid) + ' = ' + ($nextValid) + '; ';
127 out = it.util.cleanUpCode(out);