anyOf.js 2.9 KB

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