switch.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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) { /* istanbul ignore if */
  64. if (it.async) {
  65. out += ' throw new ValidationError([' + (__err) + ']); ';
  66. } else {
  67. out += ' validate.errors = [' + (__err) + ']; return false; ';
  68. }
  69. } else {
  70. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  71. }
  72. }
  73. out += ' var ' + ($nextValid) + ' = ' + ($sch.then) + '; ';
  74. } else {
  75. $it.schema = $sch.then;
  76. $it.schemaPath = $schemaPath + '[' + $caseIndex + '].then';
  77. $it.errSchemaPath = $errSchemaPath + '/' + $caseIndex + '/then';
  78. out += ' ' + (it.validate($it)) + ' ';
  79. $it.baseId = $currentBaseId;
  80. }
  81. out += ' } else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } } ';
  82. } else {
  83. out += ' ' + ($ifPassed) + ' = true; ';
  84. if (typeof $sch.then == 'boolean') {
  85. if ($sch.then === false) {
  86. var $$outStack = $$outStack || [];
  87. $$outStack.push(out);
  88. out = ''; /* istanbul ignore else */
  89. if (it.createErrors !== false) {
  90. out += ' { keyword: \'' + ('switch') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { caseIndex: ' + ($caseIndex) + ' } ';
  91. if (it.opts.messages !== false) {
  92. out += ' , message: \'should pass "switch" keyword validation\' ';
  93. }
  94. if (it.opts.verbose) {
  95. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  96. }
  97. out += ' } ';
  98. } else {
  99. out += ' {} ';
  100. }
  101. var __err = out;
  102. out = $$outStack.pop();
  103. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  104. if (it.async) {
  105. out += ' throw new ValidationError([' + (__err) + ']); ';
  106. } else {
  107. out += ' validate.errors = [' + (__err) + ']; return false; ';
  108. }
  109. } else {
  110. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  111. }
  112. }
  113. out += ' var ' + ($nextValid) + ' = ' + ($sch.then) + '; ';
  114. } else {
  115. $it.schema = $sch.then;
  116. $it.schemaPath = $schemaPath + '[' + $caseIndex + '].then';
  117. $it.errSchemaPath = $errSchemaPath + '/' + $caseIndex + '/then';
  118. out += ' ' + (it.validate($it)) + ' ';
  119. $it.baseId = $currentBaseId;
  120. }
  121. }
  122. $shouldContinue = $sch.continue
  123. }
  124. }
  125. out += '' + ($closingBraces) + 'var ' + ($valid) + ' = ' + ($nextValid) + '; ';
  126. out = it.util.cleanUpCode(out);
  127. return out;
  128. }