Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

103 linhas
4.1 KiB

há 4 anos
  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.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) {
  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) {
  86. /* istanbul ignore if */
  87. if (it.async) {
  88. out += ' throw new ValidationError(vErrors); ';
  89. } else {
  90. out += ' validate.errors = vErrors; return false; ';
  91. }
  92. }
  93. out += ' } ';
  94. if ($breakOnError) {
  95. out += ' else { ';
  96. }
  97. } else {
  98. if ($breakOnError) {
  99. out += ' if (true) { ';
  100. }
  101. }
  102. return out;
  103. }