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.

168 lines
7.5 KiB

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