You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.7 KiB

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