_limit.js 6.3 KB

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