not.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. module.exports = function generate_not(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 $errs = 'errs__' + $lvl;
  12. var $it = it.util.copy(it);
  13. $it.level++;
  14. var $nextValid = 'valid' + $it.level;
  15. if (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) { /* istanbul ignore if */
  51. if (it.async) {
  52. out += ' throw new ValidationError([' + (__err) + ']); ';
  53. } else {
  54. out += ' validate.errors = [' + (__err) + ']; return false; ';
  55. }
  56. } else {
  57. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  58. }
  59. out += ' } else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } ';
  60. if (it.opts.allErrors) {
  61. out += ' } ';
  62. }
  63. } else {
  64. out += ' var err = '; /* istanbul ignore else */
  65. if (it.createErrors !== false) {
  66. out += ' { keyword: \'' + ('not') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
  67. if (it.opts.messages !== false) {
  68. out += ' , message: \'should NOT be valid\' ';
  69. }
  70. if (it.opts.verbose) {
  71. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  72. }
  73. out += ' } ';
  74. } else {
  75. out += ' {} ';
  76. }
  77. out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  78. if ($breakOnError) {
  79. out += ' if (false) { ';
  80. }
  81. }
  82. return out;
  83. }