properties.jst (7651B)
1 {{# def.definitions }} 2 {{# def.errors }} 3 {{# def.setupKeyword }} 4 {{# def.setupNextLevel }} 5 6 7 {{## def.validateAdditional: 8 {{ /* additionalProperties is schema */ 9 $it.schema = $aProperties; 10 $it.schemaPath = it.schemaPath + '.additionalProperties'; 11 $it.errSchemaPath = it.errSchemaPath + '/additionalProperties'; 12 $it.errorPath = it.opts._errorDataPathProperty 13 ? it.errorPath 14 : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers); 15 var $passData = $data + '[' + $key + ']'; 16 $it.dataPathArr[$dataNxt] = $key; 17 }} 18 19 {{# def.generateSubschemaCode }} 20 {{# def.optimizeValidate }} 21 #}} 22 23 24 {{ 25 var $key = 'key' + $lvl 26 , $idx = 'idx' + $lvl 27 , $dataNxt = $it.dataLevel = it.dataLevel + 1 28 , $nextData = 'data' + $dataNxt 29 , $dataProperties = 'dataProperties' + $lvl; 30 31 var $schemaKeys = Object.keys($schema || {}) 32 , $pProperties = it.schema.patternProperties || {} 33 , $pPropertyKeys = Object.keys($pProperties) 34 , $aProperties = it.schema.additionalProperties 35 , $someProperties = $schemaKeys.length || $pPropertyKeys.length 36 , $noAdditional = $aProperties === false 37 , $additionalIsSchema = typeof $aProperties == 'object' 38 && Object.keys($aProperties).length 39 , $removeAdditional = it.opts.removeAdditional 40 , $checkAdditional = $noAdditional || $additionalIsSchema || $removeAdditional 41 , $ownProperties = it.opts.ownProperties 42 , $currentBaseId = it.baseId; 43 44 var $required = it.schema.required; 45 if ($required && !(it.opts.$data && $required.$data) && $required.length < it.opts.loopRequired) 46 var $requiredHash = it.util.toHash($required); 47 }} 48 49 50 var {{=$errs}} = errors; 51 var {{=$nextValid}} = true; 52 {{? $ownProperties }} 53 var {{=$dataProperties}} = undefined; 54 {{?}} 55 56 {{? $checkAdditional }} 57 {{# def.iterateProperties }} 58 {{? $someProperties }} 59 var isAdditional{{=$lvl}} = !(false 60 {{? $schemaKeys.length }} 61 {{? $schemaKeys.length > 8 }} 62 || validate.schema{{=$schemaPath}}.hasOwnProperty({{=$key}}) 63 {{??}} 64 {{~ $schemaKeys:$propertyKey }} 65 || {{=$key}} == {{= it.util.toQuotedString($propertyKey) }} 66 {{~}} 67 {{?}} 68 {{?}} 69 {{? $pPropertyKeys.length }} 70 {{~ $pPropertyKeys:$pProperty:$i }} 71 || {{= it.usePattern($pProperty) }}.test({{=$key}}) 72 {{~}} 73 {{?}} 74 ); 75 76 if (isAdditional{{=$lvl}}) { 77 {{?}} 78 {{? $removeAdditional == 'all' }} 79 delete {{=$data}}[{{=$key}}]; 80 {{??}} 81 {{ 82 var $currentErrorPath = it.errorPath; 83 var $additionalProperty = '\' + ' + $key + ' + \''; 84 if (it.opts._errorDataPathProperty) { 85 it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers); 86 } 87 }} 88 {{? $noAdditional }} 89 {{? $removeAdditional }} 90 delete {{=$data}}[{{=$key}}]; 91 {{??}} 92 {{=$nextValid}} = false; 93 {{ 94 var $currErrSchemaPath = $errSchemaPath; 95 $errSchemaPath = it.errSchemaPath + '/additionalProperties'; 96 }} 97 {{# def.error:'additionalProperties' }} 98 {{ $errSchemaPath = $currErrSchemaPath; }} 99 {{? $breakOnError }} break; {{?}} 100 {{?}} 101 {{?? $additionalIsSchema }} 102 {{? $removeAdditional == 'failing' }} 103 var {{=$errs}} = errors; 104 {{# def.setCompositeRule }} 105 106 {{# def.validateAdditional }} 107 108 if (!{{=$nextValid}}) { 109 errors = {{=$errs}}; 110 if (validate.errors !== null) { 111 if (errors) validate.errors.length = errors; 112 else validate.errors = null; 113 } 114 delete {{=$data}}[{{=$key}}]; 115 } 116 117 {{# def.resetCompositeRule }} 118 {{??}} 119 {{# def.validateAdditional }} 120 {{? $breakOnError }} if (!{{=$nextValid}}) break; {{?}} 121 {{?}} 122 {{?}} 123 {{ it.errorPath = $currentErrorPath; }} 124 {{?}} 125 {{? $someProperties }} 126 } 127 {{?}} 128 } 129 130 {{# def.ifResultValid }} 131 {{?}} 132 133 {{ var $useDefaults = it.opts.useDefaults && !it.compositeRule; }} 134 135 {{? $schemaKeys.length }} 136 {{~ $schemaKeys:$propertyKey }} 137 {{ var $sch = $schema[$propertyKey]; }} 138 139 {{? {{# def.nonEmptySchema:$sch}} }} 140 {{ 141 var $prop = it.util.getProperty($propertyKey) 142 , $passData = $data + $prop 143 , $hasDefault = $useDefaults && $sch.default !== undefined; 144 $it.schema = $sch; 145 $it.schemaPath = $schemaPath + $prop; 146 $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($propertyKey); 147 $it.errorPath = it.util.getPath(it.errorPath, $propertyKey, it.opts.jsonPointers); 148 $it.dataPathArr[$dataNxt] = it.util.toQuotedString($propertyKey); 149 }} 150 151 {{# def.generateSubschemaCode }} 152 153 {{? {{# def.willOptimize }} }} 154 {{ 155 $code = {{# def._optimizeValidate }}; 156 var $useData = $passData; 157 }} 158 {{??}} 159 {{ var $useData = $nextData; }} 160 var {{=$nextData}} = {{=$passData}}; 161 {{?}} 162 163 {{? $hasDefault }} 164 {{= $code }} 165 {{??}} 166 {{? $requiredHash && $requiredHash[$propertyKey] }} 167 if ({{# def.noPropertyInData }}) { 168 {{=$nextValid}} = false; 169 {{ 170 var $currentErrorPath = it.errorPath 171 , $currErrSchemaPath = $errSchemaPath 172 , $missingProperty = it.util.escapeQuotes($propertyKey); 173 if (it.opts._errorDataPathProperty) { 174 it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers); 175 } 176 $errSchemaPath = it.errSchemaPath + '/required'; 177 }} 178 {{# def.error:'required' }} 179 {{ $errSchemaPath = $currErrSchemaPath; }} 180 {{ it.errorPath = $currentErrorPath; }} 181 } else { 182 {{??}} 183 {{? $breakOnError }} 184 if ({{# def.noPropertyInData }}) { 185 {{=$nextValid}} = true; 186 } else { 187 {{??}} 188 if ({{=$useData}} !== undefined 189 {{? $ownProperties }} 190 && {{# def.isOwnProperty }} 191 {{?}} 192 ) { 193 {{?}} 194 {{?}} 195 196 {{= $code }} 197 } 198 {{?}} {{ /* $hasDefault */ }} 199 {{?}} {{ /* def.nonEmptySchema */ }} 200 201 {{# def.ifResultValid }} 202 {{~}} 203 {{?}} 204 205 {{? $pPropertyKeys.length }} 206 {{~ $pPropertyKeys:$pProperty }} 207 {{ var $sch = $pProperties[$pProperty]; }} 208 209 {{? {{# def.nonEmptySchema:$sch}} }} 210 {{ 211 $it.schema = $sch; 212 $it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty); 213 $it.errSchemaPath = it.errSchemaPath + '/patternProperties/' 214 + it.util.escapeFragment($pProperty); 215 }} 216 217 {{# def.iterateProperties }} 218 if ({{= it.usePattern($pProperty) }}.test({{=$key}})) { 219 {{ 220 $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers); 221 var $passData = $data + '[' + $key + ']'; 222 $it.dataPathArr[$dataNxt] = $key; 223 }} 224 225 {{# def.generateSubschemaCode }} 226 {{# def.optimizeValidate }} 227 228 {{? $breakOnError }} if (!{{=$nextValid}}) break; {{?}} 229 } 230 {{? $breakOnError }} else {{=$nextValid}} = true; {{?}} 231 } 232 233 {{# def.ifResultValid }} 234 {{?}} {{ /* def.nonEmptySchema */ }} 235 {{~}} 236 {{?}} 237 238 239 {{? $breakOnError }} 240 {{= $closingBraces }} 241 if ({{=$errs}} == errors) { 242 {{?}} 243 244 {{# def.cleanUp }}