_limit.js 7.2 KB

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