oneOf.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. module.exports = function generate_oneOf(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. out += 'var ' + ($errs) + ' = errors;var prevValid' + ($lvl) + ' = false;var ' + ($valid) + ' = false;';
  18. var $currentBaseId = $it.baseId;
  19. var $wasComposite = it.compositeRule;
  20. it.compositeRule = $it.compositeRule = true;
  21. var arr1 = $schema;
  22. if (arr1) {
  23. var $sch, $i = -1,
  24. l1 = arr1.length - 1;
  25. while ($i < l1) {
  26. $sch = arr1[$i += 1];
  27. if (it.util.schemaHasRules($sch, it.RULES.all)) {
  28. $it.schema = $sch;
  29. $it.schemaPath = $schemaPath + '[' + $i + ']';
  30. $it.errSchemaPath = $errSchemaPath + '/' + $i;
  31. out += ' ' + (it.validate($it)) + ' ';
  32. $it.baseId = $currentBaseId;
  33. } else {
  34. out += ' var ' + ($nextValid) + ' = true; ';
  35. }
  36. if ($i) {
  37. out += ' if (' + ($nextValid) + ' && prevValid' + ($lvl) + ') ' + ($valid) + ' = false; else { ';
  38. $closingBraces += '}';
  39. }
  40. out += ' if (' + ($nextValid) + ') ' + ($valid) + ' = prevValid' + ($lvl) + ' = true;';
  41. }
  42. }
  43. it.compositeRule = $it.compositeRule = $wasComposite;
  44. out += '' + ($closingBraces) + 'if (!' + ($valid) + ') { var err = '; /* istanbul ignore else */
  45. if (it.createErrors !== false) {
  46. out += ' { keyword: \'' + ('oneOf') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
  47. if (it.opts.messages !== false) {
  48. out += ' , message: \'should match exactly one schema in oneOf\' ';
  49. }
  50. if (it.opts.verbose) {
  51. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  52. }
  53. out += ' } ';
  54. } else {
  55. out += ' {} ';
  56. }
  57. out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  58. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  59. if (it.async) {
  60. out += ' throw new ValidationError(vErrors); ';
  61. } else {
  62. out += ' validate.errors = vErrors; return false; ';
  63. }
  64. }
  65. out += '} else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; }';
  66. if (it.opts.allErrors) {
  67. out += ' } ';
  68. }
  69. return out;
  70. }