| 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) { /* istanbul ignore if */ |
| 84 | if (it.async) { |
| 85 | out += ' throw new ValidationError([' + (__err) + ']); '; |
| 86 | } else { |
| 87 | out += ' validate.errors = [' + (__err) + ']; return false; '; |
| 88 | } |
| 89 | } else { |
| 90 | out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; |
| 91 | } |
| 92 | } else { |
| 93 | out += ' ) { '; |
| 94 | var arr2 = $deps; |
| 95 | if (arr2) { |
| 96 | var $propertyKey, i2 = -1, |
| 97 | l2 = arr2.length - 1; |
| 98 | while (i2 < l2) { |
| 99 | $propertyKey = arr2[i2 += 1]; |
| 100 | var $prop = it.util.getProperty($propertyKey), |
| 101 | $missingProperty = it.util.escapeQuotes($propertyKey), |
| 102 | $useData = $data + $prop; |
| 103 | if (it.opts._errorDataPathProperty) { |
| 104 | it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers); |
| 105 | } |
| 106 | out += ' if ( ' + ($useData) + ' === undefined '; |
| 107 | if ($ownProperties) { |
| 108 | out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') '; |
| 109 | } |
| 110 | out += ') { var err = '; /* istanbul ignore else */ |
| 111 | if (it.createErrors !== false) { |
| 112 | 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(", "))) + '\' } '; |
| 113 | if (it.opts.messages !== false) { |
| 114 | out += ' , message: \'should have '; |
| 115 | if ($deps.length == 1) { |
| 116 | out += 'property ' + (it.util.escapeQuotes($deps[0])); |
| 117 | } else { |
| 118 | out += 'properties ' + (it.util.escapeQuotes($deps.join(", "))); |
| 119 | } |
| 120 | out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' '; |
| 121 | } |
| 122 | if (it.opts.verbose) { |
| 123 | out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' '; |
| 124 | } |
| 125 | out += ' } '; |
| 126 | } else { |
| 127 | out += ' {} '; |
| 128 | } |
| 129 | out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } '; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | out += ' } '; |
| 134 | if ($breakOnError) { |
| 135 | $closingBraces += '}'; |
| 136 | out += ' else { '; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | it.errorPath = $currentErrorPath; |
| 141 | var $currentBaseId = $it.baseId; |
| 142 | for (var $property in $schemaDeps) { |
| 143 | var $sch = $schemaDeps[$property]; |
| 144 | if (it.util.schemaHasRules($sch, it.RULES.all)) { |
| 145 | out += ' ' + ($nextValid) + ' = true; if ( ' + ($data) + (it.util.getProperty($property)) + ' !== undefined '; |
| 146 | if ($ownProperties) { |
| 147 | out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($property)) + '\') '; |
| 148 | } |
| 149 | out += ') { '; |
| 150 | $it.schema = $sch; |
| 151 | $it.schemaPath = $schemaPath + it.util.getProperty($property); |
| 152 | $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($property); |
| 153 | out += ' ' + (it.validate($it)) + ' '; |
| 154 | $it.baseId = $currentBaseId; |
| 155 | out += ' } '; |
| 156 | if ($breakOnError) { |
| 157 | out += ' if (' + ($nextValid) + ') { '; |
| 158 | $closingBraces += '}'; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | if ($breakOnError) { |
| 163 | out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {'; |
| 164 | } |
| 165 | out = it.util.cleanUpCode(out); |
| 166 | return out; |
| 167 | } |