dependencies.js 7.5 KB

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