2 module.exports = function generate_if(it, $keyword, $ruleType) {
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 $data = 'data' + ($dataLvl || '');
11 var $valid = 'valid' + $lvl;
12 var $errs = 'errs__' + $lvl;
13 var $it = it.util.copy(it);
15 var $nextValid = 'valid' + $it.level;
16 var $thenSch = it.schema['then'],
17 $elseSch = it.schema['else'],
18 $thenPresent = $thenSch !== undefined && (it.opts.strictKeywords ? (typeof $thenSch == 'object' && Object.keys($thenSch).length > 0) || $thenSch === false : it.util.schemaHasRules($thenSch, it.RULES.all)),
19 $elsePresent = $elseSch !== undefined && (it.opts.strictKeywords ? (typeof $elseSch == 'object' && Object.keys($elseSch).length > 0) || $elseSch === false : it.util.schemaHasRules($elseSch, it.RULES.all)),
20 $currentBaseId = $it.baseId;
21 if ($thenPresent || $elsePresent) {
23 $it.createErrors = false;
25 $it.schemaPath = $schemaPath;
26 $it.errSchemaPath = $errSchemaPath;
27 out += ' var ' + ($errs) + ' = errors; var ' + ($valid) + ' = true; ';
28 var $wasComposite = it.compositeRule;
29 it.compositeRule = $it.compositeRule = true;
30 out += ' ' + (it.validate($it)) + ' ';
31 $it.baseId = $currentBaseId;
32 $it.createErrors = true;
33 out += ' errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } ';
34 it.compositeRule = $it.compositeRule = $wasComposite;
36 out += ' if (' + ($nextValid) + ') { ';
37 $it.schema = it.schema['then'];
38 $it.schemaPath = it.schemaPath + '.then';
39 $it.errSchemaPath = it.errSchemaPath + '/then';
40 out += ' ' + (it.validate($it)) + ' ';
41 $it.baseId = $currentBaseId;
42 out += ' ' + ($valid) + ' = ' + ($nextValid) + '; ';
43 if ($thenPresent && $elsePresent) {
44 $ifClause = 'ifClause' + $lvl;
45 out += ' var ' + ($ifClause) + ' = \'then\'; ';
47 $ifClause = '\'then\'';
54 out += ' if (!' + ($nextValid) + ') { ';
57 $it.schema = it.schema['else'];
58 $it.schemaPath = it.schemaPath + '.else';
59 $it.errSchemaPath = it.errSchemaPath + '/else';
60 out += ' ' + (it.validate($it)) + ' ';
61 $it.baseId = $currentBaseId;
62 out += ' ' + ($valid) + ' = ' + ($nextValid) + '; ';
63 if ($thenPresent && $elsePresent) {
64 $ifClause = 'ifClause' + $lvl;
65 out += ' var ' + ($ifClause) + ' = \'else\'; ';
67 $ifClause = '\'else\'';
71 out += ' if (!' + ($valid) + ') { var err = '; /* istanbul ignore else */
72 if (it.createErrors !== false) {
73 out += ' { keyword: \'' + ('if') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { failingKeyword: ' + ($ifClause) + ' } ';
74 if (it.opts.messages !== false) {
75 out += ' , message: \'should match "\' + ' + ($ifClause) + ' + \'" schema\' ';
77 if (it.opts.verbose) {
78 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
84 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
85 if (!it.compositeRule && $breakOnError) {
86 /* istanbul ignore if */
88 out += ' throw new ValidationError(vErrors); ';
90 out += ' validate.errors = vErrors; return false; ';
99 out += ' if (true) { ';