_limit.js 6.9 KB

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