propertyNames.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. out += 'var ' + ($errs) + ' = errors;';
  17. if ((it.opts.strictKeywords ? typeof $schema == 'object' && Object.keys($schema).length > 0 : it.util.schemaHasRules($schema, it.RULES.all))) {
  18. $it.schema = $schema;
  19. $it.schemaPath = $schemaPath;
  20. $it.errSchemaPath = $errSchemaPath;
  21. var $key = 'key' + $lvl,
  22. $idx = 'idx' + $lvl,
  23. $i = 'i' + $lvl,
  24. $invalidName = '\' + ' + $key + ' + \'',
  25. $dataNxt = $it.dataLevel = it.dataLevel + 1,
  26. $nextData = 'data' + $dataNxt,
  27. $dataProperties = 'dataProperties' + $lvl,
  28. $ownProperties = it.opts.ownProperties,
  29. $currentBaseId = it.baseId;
  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) {
  65. /* istanbul ignore if */
  66. if (it.async) {
  67. out += ' throw new ValidationError(vErrors); ';
  68. } else {
  69. out += ' validate.errors = vErrors; return false; ';
  70. }
  71. }
  72. if ($breakOnError) {
  73. out += ' break; ';
  74. }
  75. out += ' } }';
  76. }
  77. if ($breakOnError) {
  78. out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
  79. }
  80. out = it.util.cleanUpCode(out);
  81. return out;
  82. }