_limit.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use strict';
  2. module.exports = function generate__limit(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 $errorKeyword;
  11. var $data = 'data' + ($dataLvl || '');
  12. var $isData = it.opts.v5 && $schema && $schema.$data,
  13. $schemaValue;
  14. if ($isData) {
  15. out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
  16. $schemaValue = 'schema' + $lvl;
  17. } else {
  18. $schemaValue = $schema;
  19. }
  20. var $isMax = $keyword == 'maximum',
  21. $exclusiveKeyword = $isMax ? 'exclusiveMaximum' : 'exclusiveMinimum',
  22. $schemaExcl = it.schema[$exclusiveKeyword],
  23. $isDataExcl = it.opts.v5 && $schemaExcl && $schemaExcl.$data,
  24. $op = $isMax ? '<' : '>',
  25. $notOp = $isMax ? '>' : '<';
  26. if ($isDataExcl) {
  27. var $schemaValueExcl = it.util.getData($schemaExcl.$data, $dataLvl, it.dataPathArr),
  28. $exclusive = 'exclusive' + $lvl,
  29. $opExpr = 'op' + $lvl,
  30. $opStr = '\' + ' + $opExpr + ' + \'';
  31. out += ' var schemaExcl' + ($lvl) + ' = ' + ($schemaValueExcl) + '; ';
  32. $schemaValueExcl = 'schemaExcl' + $lvl;
  33. out += ' var exclusive' + ($lvl) + '; if (typeof ' + ($schemaValueExcl) + ' != \'boolean\' && typeof ' + ($schemaValueExcl) + ' != \'undefined\') { ';
  34. var $errorKeyword = $exclusiveKeyword;
  35. var $$outStack = $$outStack || [];
  36. $$outStack.push(out);
  37. out = ''; /* istanbul ignore else */
  38. if (it.createErrors !== false) {
  39. out += ' { keyword: \'' + ($errorKeyword || '_exclusiveLimit') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
  40. if (it.opts.messages !== false) {
  41. out += ' , message: \'' + ($exclusiveKeyword) + ' should be boolean\' ';
  42. }
  43. if (it.opts.verbose) {
  44. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  45. }
  46. out += ' } ';
  47. } else {
  48. out += ' {} ';
  49. }
  50. var __err = out;
  51. out = $$outStack.pop();
  52. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  53. if (it.async) {
  54. out += ' throw new ValidationError([' + (__err) + ']); ';
  55. } else {
  56. out += ' validate.errors = [' + (__err) + ']; return false; ';
  57. }
  58. } else {
  59. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  60. }
  61. out += ' } else if( ';
  62. if ($isData) {
  63. out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || ';
  64. }
  65. out += ' ((exclusive' + ($lvl) + ' = ' + ($schemaValueExcl) + ' === true) ? ' + ($data) + ' ' + ($notOp) + '= ' + ($schemaValue) + ' : ' + ($data) + ' ' + ($notOp) + ' ' + ($schemaValue) + ') || ' + ($data) + ' !== ' + ($data) + ') { var op' + ($lvl) + ' = exclusive' + ($lvl) + ' ? \'' + ($op) + '\' : \'' + ($op) + '=\';';
  66. } else {
  67. var $exclusive = $schemaExcl === true,
  68. $opStr = $op;
  69. if (!$exclusive) $opStr += '=';
  70. var $opExpr = '\'' + $opStr + '\'';
  71. out += ' if ( ';
  72. if ($isData) {
  73. out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || ';
  74. }
  75. out += ' ' + ($data) + ' ' + ($notOp);
  76. if ($exclusive) {
  77. out += '=';
  78. }
  79. out += ' ' + ($schemaValue) + ' || ' + ($data) + ' !== ' + ($data) + ') {';
  80. }
  81. var $errorKeyword = $keyword;
  82. var $$outStack = $$outStack || [];
  83. $$outStack.push(out);
  84. out = ''; /* istanbul ignore else */
  85. if (it.createErrors !== false) {
  86. out += ' { keyword: \'' + ($errorKeyword || '_limit') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { comparison: ' + ($opExpr) + ', limit: ' + ($schemaValue) + ', exclusive: ' + ($exclusive) + ' } ';
  87. if (it.opts.messages !== false) {
  88. out += ' , message: \'should be ' + ($opStr) + ' ';
  89. if ($isData) {
  90. out += '\' + ' + ($schemaValue);
  91. } else {
  92. out += '' + ($schema) + '\'';
  93. }
  94. }
  95. if (it.opts.verbose) {
  96. out += ' , schema: ';
  97. if ($isData) {
  98. out += 'validate.schema' + ($schemaPath);
  99. } else {
  100. out += '' + ($schema);
  101. }
  102. out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  103. }
  104. out += ' } ';
  105. } else {
  106. out += ' {} ';
  107. }
  108. var __err = out;
  109. out = $$outStack.pop();
  110. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  111. if (it.async) {
  112. out += ' throw new ValidationError([' + (__err) + ']); ';
  113. } else {
  114. out += ' validate.errors = [' + (__err) + ']; return false; ';
  115. }
  116. } else {
  117. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  118. }
  119. out += ' } ';
  120. if ($breakOnError) {
  121. out += ' else { ';
  122. }
  123. return out;
  124. }