_limit.js 6.7 KB

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