not.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. module.exports = function generate_not(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 $errs = 'errs__' + $lvl;
  12. var $it = it.util.copy(it);
  13. $it.level++;
  14. var $nextValid = 'valid' + $it.level;
  15. if ((it.opts.strictKeywords ? (typeof $schema == 'object' && Object.keys($schema).length > 0) || $schema === false : it.util.schemaHasRules($schema, it.RULES.all))) {
  16. $it.schema = $schema;
  17. $it.schemaPath = $schemaPath;
  18. $it.errSchemaPath = $errSchemaPath;
  19. out += ' var ' + ($errs) + ' = errors; ';
  20. var $wasComposite = it.compositeRule;
  21. it.compositeRule = $it.compositeRule = true;
  22. $it.createErrors = false;
  23. var $allErrorsOption;
  24. if ($it.opts.allErrors) {
  25. $allErrorsOption = $it.opts.allErrors;
  26. $it.opts.allErrors = false;
  27. }
  28. out += ' ' + (it.validate($it)) + ' ';
  29. $it.createErrors = true;
  30. if ($allErrorsOption) $it.opts.allErrors = $allErrorsOption;
  31. it.compositeRule = $it.compositeRule = $wasComposite;
  32. out += ' if (' + ($nextValid) + ') { ';
  33. var $$outStack = $$outStack || [];
  34. $$outStack.push(out);
  35. out = ''; /* istanbul ignore else */
  36. if (it.createErrors !== false) {
  37. out += ' { keyword: \'' + ('not') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
  38. if (it.opts.messages !== false) {
  39. out += ' , message: \'should NOT be valid\' ';
  40. }
  41. if (it.opts.verbose) {
  42. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  43. }
  44. out += ' } ';
  45. } else {
  46. out += ' {} ';
  47. }
  48. var __err = out;
  49. out = $$outStack.pop();
  50. if (!it.compositeRule && $breakOnError) {
  51. /* istanbul ignore if */
  52. if (it.async) {
  53. out += ' throw new ValidationError([' + (__err) + ']); ';
  54. } else {
  55. out += ' validate.errors = [' + (__err) + ']; return false; ';
  56. }
  57. } else {
  58. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  59. }
  60. out += ' } else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } ';
  61. if (it.opts.allErrors) {
  62. out += ' } ';
  63. }
  64. } else {
  65. out += ' var err = '; /* istanbul ignore else */
  66. if (it.createErrors !== false) {
  67. out += ' { keyword: \'' + ('not') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
  68. if (it.opts.messages !== false) {
  69. out += ' , message: \'should NOT be valid\' ';
  70. }
  71. if (it.opts.verbose) {
  72. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  73. }
  74. out += ' } ';
  75. } else {
  76. out += ' {} ';
  77. }
  78. out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  79. if ($breakOnError) {
  80. out += ' if (false) { ';
  81. }
  82. }
  83. return out;
  84. }