if.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. module.exports = function generate_if(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. $it.level++;
  15. var $nextValid = 'valid' + $it.level;
  16. var $thenSch = it.schema['then'],
  17. $elseSch = it.schema['else'],
  18. $thenPresent = $thenSch !== undefined && it.util.schemaHasRules($thenSch, it.RULES.all),
  19. $elsePresent = $elseSch !== undefined && it.util.schemaHasRules($elseSch, it.RULES.all),
  20. $currentBaseId = $it.baseId;
  21. if ($thenPresent || $elsePresent) {
  22. var $ifClause;
  23. $it.createErrors = false;
  24. $it.schema = $schema;
  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;
  35. if ($thenPresent) {
  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\'; ';
  46. } else {
  47. $ifClause = '\'then\'';
  48. }
  49. out += ' } ';
  50. if ($elsePresent) {
  51. out += ' else { ';
  52. }
  53. } else {
  54. out += ' if (!' + ($nextValid) + ') { ';
  55. }
  56. if ($elsePresent) {
  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\'; ';
  66. } else {
  67. $ifClause = '\'else\'';
  68. }
  69. out += ' } ';
  70. }
  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\' ';
  76. }
  77. if (it.opts.verbose) {
  78. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  79. }
  80. out += ' } ';
  81. } else {
  82. out += ' {} ';
  83. }
  84. out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  85. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  86. if (it.async) {
  87. out += ' throw new ValidationError(vErrors); ';
  88. } else {
  89. out += ' validate.errors = vErrors; return false; ';
  90. }
  91. }
  92. out += ' } ';
  93. if ($breakOnError) {
  94. out += ' else { ';
  95. }
  96. out = it.util.cleanUpCode(out);
  97. } else {
  98. if ($breakOnError) {
  99. out += ' if (true) { ';
  100. }
  101. }
  102. return out;
  103. }