_limitLength.js 2.7 KB

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