dependencies.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 'use strict';
  2. module.exports = function generate_dependencies(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 $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. var $schemaDeps = {},
  17. $propertyDeps = {};
  18. for ($property in $schema) {
  19. var $sch = $schema[$property];
  20. var $deps = Array.isArray($sch) ? $propertyDeps : $schemaDeps;
  21. $deps[$property] = $sch;
  22. }
  23. out += 'var ' + ($errs) + ' = errors;';
  24. var $currentErrorPath = it.errorPath;
  25. out += 'var missing' + ($lvl) + ';';
  26. for (var $property in $propertyDeps) {
  27. $deps = $propertyDeps[$property];
  28. out += ' if (' + ($data) + (it.util.getProperty($property)) + ' !== undefined ';
  29. if ($breakOnError) {
  30. out += ' && ( ';
  31. var arr1 = $deps;
  32. if (arr1) {
  33. var _$property, $i = -1,
  34. l1 = arr1.length - 1;
  35. while ($i < l1) {
  36. _$property = arr1[$i += 1];
  37. if ($i) {
  38. out += ' || ';
  39. }
  40. var $prop = it.util.getProperty(_$property);
  41. out += ' ( ' + ($data) + ($prop) + ' === undefined && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? _$property : $prop)) + ') ) ';
  42. }
  43. }
  44. out += ')) { ';
  45. var $propertyPath = 'missing' + $lvl,
  46. $missingProperty = '\' + ' + $propertyPath + ' + \'';
  47. if (it.opts._errorDataPathProperty) {
  48. it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath;
  49. }
  50. var $$outStack = $$outStack || [];
  51. $$outStack.push(out);
  52. out = ''; /* istanbul ignore else */
  53. if (it.createErrors !== false) {
  54. out += ' { keyword: \'' + ('dependencies') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { property: \'' + (it.util.escapeQuotes($property)) + '\', missingProperty: \'' + ($missingProperty) + '\', depsCount: ' + ($deps.length) + ', deps: \'' + (it.util.escapeQuotes($deps.length == 1 ? $deps[0] : $deps.join(", "))) + '\' } ';
  55. if (it.opts.messages !== false) {
  56. out += ' , message: \'should have ';
  57. if ($deps.length == 1) {
  58. out += 'property ' + (it.util.escapeQuotes($deps[0]));
  59. } else {
  60. out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));
  61. }
  62. out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';
  63. }
  64. if (it.opts.verbose) {
  65. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  66. }
  67. out += ' } ';
  68. } else {
  69. out += ' {} ';
  70. }
  71. var __err = out;
  72. out = $$outStack.pop();
  73. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  74. if (it.async) {
  75. out += ' throw new ValidationError([' + (__err) + ']); ';
  76. } else {
  77. out += ' validate.errors = [' + (__err) + ']; return false; ';
  78. }
  79. } else {
  80. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  81. }
  82. } else {
  83. out += ' ) { ';
  84. var arr2 = $deps;
  85. if (arr2) {
  86. var $reqProperty, i2 = -1,
  87. l2 = arr2.length - 1;
  88. while (i2 < l2) {
  89. $reqProperty = arr2[i2 += 1];
  90. var $prop = it.util.getProperty($reqProperty),
  91. $missingProperty = it.util.escapeQuotes($reqProperty);
  92. if (it.opts._errorDataPathProperty) {
  93. it.errorPath = it.util.getPath($currentErrorPath, $reqProperty, it.opts.jsonPointers);
  94. }
  95. out += ' if (' + ($data) + ($prop) + ' === undefined) { var err = '; /* istanbul ignore else */
  96. if (it.createErrors !== false) {
  97. out += ' { keyword: \'' + ('dependencies') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { property: \'' + (it.util.escapeQuotes($property)) + '\', missingProperty: \'' + ($missingProperty) + '\', depsCount: ' + ($deps.length) + ', deps: \'' + (it.util.escapeQuotes($deps.length == 1 ? $deps[0] : $deps.join(", "))) + '\' } ';
  98. if (it.opts.messages !== false) {
  99. out += ' , message: \'should have ';
  100. if ($deps.length == 1) {
  101. out += 'property ' + (it.util.escapeQuotes($deps[0]));
  102. } else {
  103. out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));
  104. }
  105. out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';
  106. }
  107. if (it.opts.verbose) {
  108. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  109. }
  110. out += ' } ';
  111. } else {
  112. out += ' {} ';
  113. }
  114. out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';
  115. }
  116. }
  117. }
  118. out += ' } ';
  119. if ($breakOnError) {
  120. $closingBraces += '}';
  121. out += ' else { ';
  122. }
  123. }
  124. it.errorPath = $currentErrorPath;
  125. var $currentBaseId = $it.baseId;
  126. for (var $property in $schemaDeps) {
  127. var $sch = $schemaDeps[$property];
  128. if (it.util.schemaHasRules($sch, it.RULES.all)) {
  129. out += ' ' + ($nextValid) + ' = true; if (' + ($data) + (it.util.getProperty($property)) + ' !== undefined) { ';
  130. $it.schema = $sch;
  131. $it.schemaPath = $schemaPath + it.util.getProperty($property);
  132. $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($property);
  133. out += ' ' + (it.validate($it)) + ' ';
  134. $it.baseId = $currentBaseId;
  135. out += ' } ';
  136. if ($breakOnError) {
  137. out += ' if (' + ($nextValid) + ') { ';
  138. $closingBraces += '}';
  139. }
  140. }
  141. }
  142. if ($breakOnError) {
  143. out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
  144. }
  145. out = it.util.cleanUpCode(out);
  146. return out;
  147. }