_limit.js 6.9 KB

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