oneOf.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';
  2. module.exports = function generate_oneOf(it, $keyword) {
  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) + ') { ';
  45. var $$outStack = $$outStack || [];
  46. $$outStack.push(out);
  47. out = ''; /* istanbul ignore else */
  48. if (it.createErrors !== false) {
  49. out += ' { keyword: \'' + ('oneOf') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
  50. if (it.opts.messages !== false) {
  51. out += ' , message: \'should match exactly one schema in oneOf\' ';
  52. }
  53. if (it.opts.verbose) {
  54. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  55. }
  56. out += ' } ';
  57. } else {
  58. out += ' {} ';
  59. }
  60. var __err = out;
  61. out = $$outStack.pop();
  62. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  63. if (it.async) {
  64. out += ' throw new ValidationError([' + (__err) + ']); ';
  65. } else {
  66. out += ' validate.errors = [' + (__err) + ']; return false; ';
  67. }
  68. } else {
  69. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  70. }
  71. out += '} else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; }';
  72. if (it.opts.allErrors) {
  73. out += ' } ';
  74. }
  75. return out;
  76. }