_limitLength.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. module.exports = function generate__limitLength(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 $isData = it.opts.v5 && $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) { /* istanbul ignore if */
  68. if (it.async) {
  69. out += ' throw new ValidationError([' + (__err) + ']); ';
  70. } else {
  71. out += ' validate.errors = [' + (__err) + ']; return false; ';
  72. }
  73. } else {
  74. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  75. }
  76. out += '} ';
  77. if ($breakOnError) {
  78. out += ' else { ';
  79. }
  80. return out;
  81. }