contains.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. module.exports = function generate_contains(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 $idx = 'i' + $lvl,
  18. $dataNxt = $it.dataLevel = it.dataLevel + 1,
  19. $nextData = 'data' + $dataNxt,
  20. $currentBaseId = it.baseId,
  21. $nonEmptySchema = it.util.schemaHasRules($schema, it.RULES.all);
  22. out += 'var ' + ($errs) + ' = errors;var ' + ($valid) + ';';
  23. if ($nonEmptySchema) {
  24. var $wasComposite = it.compositeRule;
  25. it.compositeRule = $it.compositeRule = true;
  26. $it.schema = $schema;
  27. $it.schemaPath = $schemaPath;
  28. $it.errSchemaPath = $errSchemaPath;
  29. out += ' var ' + ($nextValid) + ' = false; for (var ' + ($idx) + ' = 0; ' + ($idx) + ' < ' + ($data) + '.length; ' + ($idx) + '++) { ';
  30. $it.errorPath = it.util.getPathExpr(it.errorPath, $idx, it.opts.jsonPointers, true);
  31. var $passData = $data + '[' + $idx + ']';
  32. $it.dataPathArr[$dataNxt] = $idx;
  33. var $code = it.validate($it);
  34. $it.baseId = $currentBaseId;
  35. if (it.util.varOccurences($code, $nextData) < 2) {
  36. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  37. } else {
  38. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  39. }
  40. out += ' if (' + ($nextValid) + ') break; } ';
  41. it.compositeRule = $it.compositeRule = $wasComposite;
  42. out += ' ' + ($closingBraces) + ' if (!' + ($nextValid) + ') {';
  43. } else {
  44. out += ' if (' + ($data) + '.length == 0) {';
  45. }
  46. var $$outStack = $$outStack || [];
  47. $$outStack.push(out);
  48. out = ''; /* istanbul ignore else */
  49. if (it.createErrors !== false) {
  50. out += ' { keyword: \'' + ('contains') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
  51. if (it.opts.messages !== false) {
  52. out += ' , message: \'should contain a valid item\' ';
  53. }
  54. if (it.opts.verbose) {
  55. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  56. }
  57. out += ' } ';
  58. } else {
  59. out += ' {} ';
  60. }
  61. var __err = out;
  62. out = $$outStack.pop();
  63. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  64. if (it.async) {
  65. out += ' throw new ValidationError([' + (__err) + ']); ';
  66. } else {
  67. out += ' validate.errors = [' + (__err) + ']; return false; ';
  68. }
  69. } else {
  70. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  71. }
  72. out += ' } else { ';
  73. if ($nonEmptySchema) {
  74. out += ' errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } ';
  75. }
  76. if (it.opts.allErrors) {
  77. out += ' } ';
  78. }
  79. out = it.util.cleanUpCode(out);
  80. return out;
  81. }