_limitLength.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. module.exports = function generate__limitLength(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. if (!($isData || typeof $schema == 'number')) {
  21. throw new Error($keyword + ' must be number');
  22. }
  23. var $op = $keyword == 'maxLength' ? '>' : '<';
  24. out += 'if ( ';
  25. if ($isData) {
  26. out += ' (' + ($schemaValue) + ' !== undefined && typeof ' + ($schemaValue) + ' != \'number\') || ';
  27. }
  28. if (it.opts.unicode === false) {
  29. out += ' ' + ($data) + '.length ';
  30. } else {
  31. out += ' ucs2length(' + ($data) + ') ';
  32. }
  33. out += ' ' + ($op) + ' ' + ($schemaValue) + ') { ';
  34. var $errorKeyword = $keyword;
  35. var $$outStack = $$outStack || [];
  36. $$outStack.push(out);
  37. out = ''; /* istanbul ignore else */
  38. if (it.createErrors !== false) {
  39. out += ' { keyword: \'' + ($errorKeyword || '_limitLength') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { limit: ' + ($schemaValue) + ' } ';
  40. if (it.opts.messages !== false) {
  41. out += ' , message: \'should NOT be ';
  42. if ($keyword == 'maxLength') {
  43. out += 'longer';
  44. } else {
  45. out += 'shorter';
  46. }
  47. out += ' than ';
  48. if ($isData) {
  49. out += '\' + ' + ($schemaValue) + ' + \'';
  50. } else {
  51. out += '' + ($schema);
  52. }
  53. out += ' characters\' ';
  54. }
  55. if (it.opts.verbose) {
  56. out += ' , schema: ';
  57. if ($isData) {
  58. out += 'validate.schema' + ($schemaPath);
  59. } else {
  60. out += '' + ($schema);
  61. }
  62. out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  63. }
  64. out += ' } ';
  65. } else {
  66. out += ' {} ';
  67. }
  68. var __err = out;
  69. out = $$outStack.pop();
  70. if (!it.compositeRule && $breakOnError) {
  71. /* istanbul ignore if */
  72. if (it.async) {
  73. out += ' throw new ValidationError([' + (__err) + ']); ';
  74. } else {
  75. out += ' validate.errors = [' + (__err) + ']; return false; ';
  76. }
  77. } else {
  78. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  79. }
  80. out += '} ';
  81. if ($breakOnError) {
  82. out += ' else { ';
  83. }
  84. return out;
  85. }