propertyNames.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. module.exports = function generate_propertyNames(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 $data = 'data' + ($dataLvl || '');
  11. var $errs = 'errs__' + $lvl;
  12. var $it = it.util.copy(it);
  13. var $closingBraces = '';
  14. $it.level++;
  15. var $nextValid = 'valid' + $it.level;
  16. if (it.util.schemaHasRules($schema, it.RULES.all)) {
  17. $it.schema = $schema;
  18. $it.schemaPath = $schemaPath;
  19. $it.errSchemaPath = $errSchemaPath;
  20. var $key = 'key' + $lvl,
  21. $idx = 'idx' + $lvl,
  22. $i = 'i' + $lvl,
  23. $invalidName = '\' + ' + $key + ' + \'',
  24. $dataNxt = $it.dataLevel = it.dataLevel + 1,
  25. $nextData = 'data' + $dataNxt,
  26. $dataProperties = 'dataProperties' + $lvl,
  27. $ownProperties = it.opts.ownProperties,
  28. $currentBaseId = it.baseId;
  29. out += ' var ' + ($errs) + ' = errors; ';
  30. if ($ownProperties) {
  31. out += ' var ' + ($dataProperties) + ' = undefined; ';
  32. }
  33. if ($ownProperties) {
  34. out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
  35. } else {
  36. out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
  37. }
  38. out += ' var startErrs' + ($lvl) + ' = errors; ';
  39. var $passData = $key;
  40. var $wasComposite = it.compositeRule;
  41. it.compositeRule = $it.compositeRule = true;
  42. var $code = it.validate($it);
  43. $it.baseId = $currentBaseId;
  44. if (it.util.varOccurences($code, $nextData) < 2) {
  45. out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
  46. } else {
  47. out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
  48. }
  49. it.compositeRule = $it.compositeRule = $wasComposite;
  50. out += ' if (!' + ($nextValid) + ') { for (var ' + ($i) + '=startErrs' + ($lvl) + '; ' + ($i) + '<errors; ' + ($i) + '++) { vErrors[' + ($i) + '].propertyName = ' + ($key) + '; } var err = '; /* istanbul ignore else */
  51. if (it.createErrors !== false) {
  52. out += ' { keyword: \'' + ('propertyNames') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { propertyName: \'' + ($invalidName) + '\' } ';
  53. if (it.opts.messages !== false) {
  54. out += ' , message: \'property name \\\'' + ($invalidName) + '\\\' is invalid\' ';
  55. }
  56. if (it.opts.verbose) {
  57. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  58. }
  59. out += ' } ';
  60. } else {
  61. out += ' {} ';
  62. }
  63. out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  64. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  65. if (it.async) {
  66. out += ' throw new ValidationError(vErrors); ';
  67. } else {
  68. out += ' validate.errors = vErrors; return false; ';
  69. }
  70. }
  71. if ($breakOnError) {
  72. out += ' break; ';
  73. }
  74. out += ' } }';
  75. }
  76. if ($breakOnError) {
  77. out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
  78. }
  79. out = it.util.cleanUpCode(out);
  80. return out;
  81. }