_formatLimit.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. 'use strict';
  2. module.exports = function generate__formatLimit(it, $keyword) {
  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 $valid = 'valid' + $lvl;
  13. out += 'var ' + ($valid) + ' = undefined;';
  14. if (it.opts.format === false) {
  15. out += ' ' + ($valid) + ' = true; ';
  16. return out;
  17. }
  18. var $schemaFormat = it.schema.format,
  19. $isDataFormat = it.opts.v5 && $schemaFormat.$data,
  20. $closingBraces = '';
  21. if ($isDataFormat) {
  22. var $schemaValueFormat = it.util.getData($schemaFormat.$data, $dataLvl, it.dataPathArr),
  23. $format = 'format' + $lvl,
  24. $compare = 'compare' + $lvl;
  25. out += ' var ' + ($format) + ' = formats[' + ($schemaValueFormat) + '] , ' + ($compare) + ' = ' + ($format) + ' && ' + ($format) + '.compare;';
  26. } else {
  27. var $format = it.formats[$schemaFormat];
  28. if (!($format && $format.compare)) {
  29. out += ' ' + ($valid) + ' = true; ';
  30. return out;
  31. }
  32. var $compare = 'formats' + it.util.getProperty($schemaFormat) + '.compare';
  33. }
  34. var $isMax = $keyword == 'formatMaximum',
  35. $exclusiveKeyword = 'formatExclusive' + ($isMax ? 'Maximum' : 'Minimum'),
  36. $schemaExcl = it.schema[$exclusiveKeyword],
  37. $isDataExcl = it.opts.v5 && $schemaExcl && $schemaExcl.$data,
  38. $op = $isMax ? '<' : '>',
  39. $result = 'result' + $lvl;
  40. var $isData = it.opts.v5 && $schema && $schema.$data,
  41. $schemaValue;
  42. if ($isData) {
  43. out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
  44. $schemaValue = 'schema' + $lvl;
  45. } else {
  46. $schemaValue = $schema;
  47. }
  48. if ($isDataExcl) {
  49. var $schemaValueExcl = it.util.getData($schemaExcl.$data, $dataLvl, it.dataPathArr),
  50. $exclusive = 'exclusive' + $lvl,
  51. $opExpr = 'op' + $lvl,
  52. $opStr = '\' + ' + $opExpr + ' + \'';
  53. out += ' var schemaExcl' + ($lvl) + ' = ' + ($schemaValueExcl) + '; ';
  54. $schemaValueExcl = 'schemaExcl' + $lvl;
  55. out += ' if (typeof ' + ($schemaValueExcl) + ' != \'boolean\' && ' + ($schemaValueExcl) + ' !== undefined) { ' + ($valid) + ' = false; ';
  56. var $errorKeyword = $exclusiveKeyword;
  57. var $$outStack = $$outStack || [];
  58. $$outStack.push(out);
  59. out = ''; /* istanbul ignore else */
  60. if (it.createErrors !== false) {
  61. out += ' { keyword: \'' + ($errorKeyword || '_formatExclusiveLimit') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: {} ';
  62. if (it.opts.messages !== false) {
  63. out += ' , message: \'' + ($exclusiveKeyword) + ' should be boolean\' ';
  64. }
  65. if (it.opts.verbose) {
  66. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  67. }
  68. out += ' } ';
  69. } else {
  70. out += ' {} ';
  71. }
  72. var __err = out;
  73. out = $$outStack.pop();
  74. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  75. if (it.async) {
  76. out += ' throw new ValidationError([' + (__err) + ']); ';
  77. } else {
  78. out += ' validate.errors = [' + (__err) + ']; return false; ';
  79. }
  80. } else {
  81. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  82. }
  83. out += ' } ';
  84. if ($breakOnError) {
  85. $closingBraces += '}';
  86. out += ' else { ';
  87. }
  88. if ($isData) {
  89. out += ' if (' + ($schemaValue) + ' === undefined) ' + ($valid) + ' = true; else if (typeof ' + ($schemaValue) + ' != \'string\') ' + ($valid) + ' = false; else { ';
  90. $closingBraces += '}';
  91. }
  92. if ($isDataFormat) {
  93. out += ' if (!' + ($compare) + ') ' + ($valid) + ' = true; else { ';
  94. $closingBraces += '}';
  95. }
  96. out += ' var ' + ($result) + ' = ' + ($compare) + '(' + ($data) + ', ';
  97. if ($isData) {
  98. out += '' + ($schemaValue);
  99. } else {
  100. out += '' + (it.util.toQuotedString($schema));
  101. }
  102. out += ' ); if (' + ($result) + ' === undefined) ' + ($valid) + ' = false; var ' + ($exclusive) + ' = ' + ($schemaValueExcl) + ' === true; if (' + ($valid) + ' === undefined) { ' + ($valid) + ' = ' + ($exclusive) + ' ? ' + ($result) + ' ' + ($op) + ' 0 : ' + ($result) + ' ' + ($op) + '= 0; } if (!' + ($valid) + ') var op' + ($lvl) + ' = ' + ($exclusive) + ' ? \'' + ($op) + '\' : \'' + ($op) + '=\';';
  103. } else {
  104. var $exclusive = $schemaExcl === true,
  105. $opStr = $op;
  106. if (!$exclusive) $opStr += '=';
  107. var $opExpr = '\'' + $opStr + '\'';
  108. if ($isData) {
  109. out += ' if (' + ($schemaValue) + ' === undefined) ' + ($valid) + ' = true; else if (typeof ' + ($schemaValue) + ' != \'string\') ' + ($valid) + ' = false; else { ';
  110. $closingBraces += '}';
  111. }
  112. if ($isDataFormat) {
  113. out += ' if (!' + ($compare) + ') ' + ($valid) + ' = true; else { ';
  114. $closingBraces += '}';
  115. }
  116. out += ' var ' + ($result) + ' = ' + ($compare) + '(' + ($data) + ', ';
  117. if ($isData) {
  118. out += '' + ($schemaValue);
  119. } else {
  120. out += '' + (it.util.toQuotedString($schema));
  121. }
  122. out += ' ); if (' + ($result) + ' === undefined) ' + ($valid) + ' = false; if (' + ($valid) + ' === undefined) ' + ($valid) + ' = ' + ($result) + ' ' + ($op);
  123. if (!$exclusive) {
  124. out += '=';
  125. }
  126. out += ' 0;';
  127. }
  128. out += '' + ($closingBraces) + 'if (!' + ($valid) + ') { ';
  129. var $errorKeyword = $keyword;
  130. var $$outStack = $$outStack || [];
  131. $$outStack.push(out);
  132. out = ''; /* istanbul ignore else */
  133. if (it.createErrors !== false) {
  134. out += ' { keyword: \'' + ($errorKeyword || '_formatLimit') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { comparison: ' + ($opExpr) + ', limit: ';
  135. if ($isData) {
  136. out += '' + ($schemaValue);
  137. } else {
  138. out += '' + (it.util.toQuotedString($schema));
  139. }
  140. out += ' , exclusive: ' + ($exclusive) + ' } ';
  141. if (it.opts.messages !== false) {
  142. out += ' , message: \'should be ' + ($opStr) + ' "';
  143. if ($isData) {
  144. out += '\' + ' + ($schemaValue) + ' + \'';
  145. } else {
  146. out += '' + (it.util.escapeQuotes($schema));
  147. }
  148. out += '"\' ';
  149. }
  150. if (it.opts.verbose) {
  151. out += ' , schema: ';
  152. if ($isData) {
  153. out += 'validate.schema' + ($schemaPath);
  154. } else {
  155. out += '' + (it.util.toQuotedString($schema));
  156. }
  157. out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  158. }
  159. out += ' } ';
  160. } else {
  161. out += ' {} ';
  162. }
  163. var __err = out;
  164. out = $$outStack.pop();
  165. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  166. if (it.async) {
  167. out += ' throw new ValidationError([' + (__err) + ']); ';
  168. } else {
  169. out += ' validate.errors = [' + (__err) + ']; return false; ';
  170. }
  171. } else {
  172. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  173. }
  174. out += '}';
  175. return out;
  176. }