Projekt

Obecné

Profil

Stáhnout (5.66 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2
module.exports = function generate_switch(it, $keyword, $ruleType) {
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 $data = 'data' + ($dataLvl || '');
11
  var $valid = 'valid' + $lvl;
12
  var $errs = 'errs__' + $lvl;
13
  var $it = it.util.copy(it);
14
  var $closingBraces = '';
15
  $it.level++;
16
  var $nextValid = 'valid' + $it.level;
17
  var $ifPassed = 'ifPassed' + it.level,
18
    $currentBaseId = $it.baseId,
19
    $shouldContinue;
20
  out += 'var ' + ($ifPassed) + ';';
21
  var arr1 = $schema;
22
  if (arr1) {
23
    var $sch, $caseIndex = -1,
24
      l1 = arr1.length - 1;
25
    while ($caseIndex < l1) {
26
      $sch = arr1[$caseIndex += 1];
27
      if ($caseIndex && !$shouldContinue) {
28
        out += ' if (!' + ($ifPassed) + ') { ';
29
        $closingBraces += '}';
30
      }
31
      if ($sch.if && it.util.schemaHasRules($sch.if, it.RULES.all)) {
32
        out += ' var ' + ($errs) + ' = errors;   ';
33
        var $wasComposite = it.compositeRule;
34
        it.compositeRule = $it.compositeRule = true;
35
        $it.createErrors = false;
36
        $it.schema = $sch.if;
37
        $it.schemaPath = $schemaPath + '[' + $caseIndex + '].if';
38
        $it.errSchemaPath = $errSchemaPath + '/' + $caseIndex + '/if';
39
        out += '  ' + (it.validate($it)) + ' ';
40
        $it.baseId = $currentBaseId;
41
        $it.createErrors = true;
42
        it.compositeRule = $it.compositeRule = $wasComposite;
43
        out += ' ' + ($ifPassed) + ' = ' + ($nextValid) + '; if (' + ($ifPassed) + ') {  ';
44
        if (typeof $sch.then == 'boolean') {
45
          if ($sch.then === false) {
46
            var $$outStack = $$outStack || [];
47
            $$outStack.push(out);
48
            out = ''; /* istanbul ignore else */
49
            if (it.createErrors !== false) {
50
              out += ' { keyword: \'' + ('switch') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { caseIndex: ' + ($caseIndex) + ' } ';
51
              if (it.opts.messages !== false) {
52
                out += ' , message: \'should pass "switch" keyword validation\' ';
53
              }
54
              if (it.opts.verbose) {
55
                out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
56
              }
57
              out += ' } ';
58
            } else {
59
              out += ' {} ';
60
            }
61
            var __err = out;
62
            out = $$outStack.pop();
63
            if (!it.compositeRule && $breakOnError) {
64
              /* 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: \'' + ('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) {
105
              /* istanbul ignore if */
106
              if (it.async) {
107
                out += ' throw new ValidationError([' + (__err) + ']); ';
108
              } else {
109
                out += ' validate.errors = [' + (__err) + ']; return false; ';
110
              }
111
            } else {
112
              out += ' var err = ' + (__err) + ';  if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
113
            }
114
          }
115
          out += ' var ' + ($nextValid) + ' = ' + ($sch.then) + '; ';
116
        } else {
117
          $it.schema = $sch.then;
118
          $it.schemaPath = $schemaPath + '[' + $caseIndex + '].then';
119
          $it.errSchemaPath = $errSchemaPath + '/' + $caseIndex + '/then';
120
          out += '  ' + (it.validate($it)) + ' ';
121
          $it.baseId = $currentBaseId;
122
        }
123
      }
124
      $shouldContinue = $sch.continue
125
    }
126
  }
127
  out += '' + ($closingBraces) + 'var ' + ($valid) + ' = ' + ($nextValid) + '; ';
128
  out = it.util.cleanUpCode(out);
129
  return out;
130
}
(4-4/4)