2 module.exports = function generate_dependencies(it, $keyword) {
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;
11 var $data = 'data' + ($dataLvl || '');
12 var $errs = 'errs__' + $lvl;
13 var $it = it.util.copy(it);
14 var $closingBraces = '';
16 var $nextValid = 'valid' + $it.level;
19 for ($property in $schema) {
20 var $sch = $schema[$property];
21 var $deps = Array.isArray($sch) ? $propertyDeps : $schemaDeps;
22 $deps[$property] = $sch;
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 out += ' if (' + ($data) + (it.util.getProperty($property)) + ' !== undefined ';
34 var _$property, $i = -1,
37 _$property = arr1[$i += 1];
41 var $prop = it.util.getProperty(_$property);
42 out += ' ( ' + ($data) + ($prop) + ' === undefined && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? _$property : $prop)) + ') ) ';
46 var $propertyPath = 'missing' + $lvl,
47 $missingProperty = '\' + ' + $propertyPath + ' + \'';
48 if (it.opts._errorDataPathProperty) {
49 it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath;
51 var $$outStack = $$outStack || [];
53 out = ''; /* istanbul ignore else */
54 if (it.createErrors !== false) {
55 out += ' { keyword: \'' + ($errorKeyword || '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(", "))) + '\' } ';
56 if (it.opts.messages !== false) {
57 out += ' , message: \'should have ';
58 if ($deps.length == 1) {
59 out += 'property ' + (it.util.escapeQuotes($deps[0]));
61 out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));
63 out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';
65 if (it.opts.verbose) {
66 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
73 out = $$outStack.pop();
74 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
76 out += ' throw new ValidationError([' + (__err) + ']); ';
78 out += ' validate.errors = [' + (__err) + ']; return false; ';
81 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
87 var $reqProperty, i2 = -1,
90 $reqProperty = arr2[i2 += 1];
91 var $prop = it.util.getProperty($reqProperty),
92 $missingProperty = it.util.escapeQuotes($reqProperty);
93 if (it.opts._errorDataPathProperty) {
94 it.errorPath = it.util.getPath($currentErrorPath, $reqProperty, it.opts.jsonPointers);
96 out += ' if (' + ($data) + ($prop) + ' === undefined) { var err = '; /* istanbul ignore else */
97 if (it.createErrors !== false) {
98 out += ' { keyword: \'' + ($errorKeyword || '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(", "))) + '\' } ';
99 if (it.opts.messages !== false) {
100 out += ' , message: \'should have ';
101 if ($deps.length == 1) {
102 out += 'property ' + (it.util.escapeQuotes($deps[0]));
104 out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));
106 out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';
108 if (it.opts.verbose) {
109 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
115 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';
121 $closingBraces += '}';
125 it.errorPath = $currentErrorPath;
126 var $currentBaseId = $it.baseId;
127 for (var $property in $schemaDeps) {
128 var $sch = $schemaDeps[$property];
129 if (it.util.schemaHasRules($sch, it.RULES.all)) {
130 out += ' ' + ($nextValid) + ' = true; if (' + ($data) + (it.util.getProperty($property)) + ' !== undefined) { ';
132 $it.schemaPath = $schemaPath + it.util.getProperty($property);
133 $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($property);
134 out += ' ' + (it.validate($it)) + ' ';
135 $it.baseId = $currentBaseId;
138 out += ' if (' + ($nextValid) + ') { ';
139 $closingBraces += '}';
144 out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
146 out = it.util.cleanUpCode(out);