2 module.exports = function generate_dependencies(it, $keyword, $ruleType) {
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 = '';
15 var $nextValid = 'valid' + $it.level;
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;
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];
31 out += ' if ( ' + ($data) + (it.util.getProperty($property)) + ' !== undefined ';
33 out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($property)) + '\') ';
39 var $propertyKey, $i = -1,
42 $propertyKey = arr1[$i += 1];
46 var $prop = it.util.getProperty($propertyKey),
47 $useData = $data + $prop;
48 out += ' ( ( ' + ($useData) + ' === undefined ';
50 out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
52 out += ') && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? $propertyKey : $prop)) + ') ) ';
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;
61 var $$outStack = $$outStack || [];
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]));
71 out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));
73 out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';
75 if (it.opts.verbose) {
76 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
83 out = $$outStack.pop();
84 if (!it.compositeRule && $breakOnError) {
85 /* istanbul ignore if */
87 out += ' throw new ValidationError([' + (__err) + ']); ';
89 out += ' validate.errors = [' + (__err) + ']; return false; ';
92 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
98 var $propertyKey, i2 = -1,
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);
108 out += ' if ( ' + ($useData) + ' === undefined ';
109 if ($ownProperties) {
110 out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
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]));
120 out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));
122 out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';
124 if (it.opts.verbose) {
125 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
131 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';
137 $closingBraces += '}';
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)) + '\') ';
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;
159 out += ' if (' + ($nextValid) + ') { ';
160 $closingBraces += '}';
165 out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';