[d565449] | 1 | 'use strict';
|
---|
| 2 | module.exports = function generate_properties(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 $key = 'key' + $lvl,
|
---|
| 17 | $idx = 'idx' + $lvl,
|
---|
| 18 | $dataNxt = $it.dataLevel = it.dataLevel + 1,
|
---|
| 19 | $nextData = 'data' + $dataNxt,
|
---|
| 20 | $dataProperties = 'dataProperties' + $lvl;
|
---|
| 21 | var $schemaKeys = Object.keys($schema || {}).filter(notProto),
|
---|
| 22 | $pProperties = it.schema.patternProperties || {},
|
---|
| 23 | $pPropertyKeys = Object.keys($pProperties).filter(notProto),
|
---|
| 24 | $aProperties = it.schema.additionalProperties,
|
---|
| 25 | $someProperties = $schemaKeys.length || $pPropertyKeys.length,
|
---|
| 26 | $noAdditional = $aProperties === false,
|
---|
| 27 | $additionalIsSchema = typeof $aProperties == 'object' && Object.keys($aProperties).length,
|
---|
| 28 | $removeAdditional = it.opts.removeAdditional,
|
---|
| 29 | $checkAdditional = $noAdditional || $additionalIsSchema || $removeAdditional,
|
---|
| 30 | $ownProperties = it.opts.ownProperties,
|
---|
| 31 | $currentBaseId = it.baseId;
|
---|
| 32 | var $required = it.schema.required;
|
---|
| 33 | if ($required && !(it.opts.$data && $required.$data) && $required.length < it.opts.loopRequired) {
|
---|
| 34 | var $requiredHash = it.util.toHash($required);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | function notProto(p) {
|
---|
| 38 | return p !== '__proto__';
|
---|
| 39 | }
|
---|
| 40 | out += 'var ' + ($errs) + ' = errors;var ' + ($nextValid) + ' = true;';
|
---|
| 41 | if ($ownProperties) {
|
---|
| 42 | out += ' var ' + ($dataProperties) + ' = undefined;';
|
---|
| 43 | }
|
---|
| 44 | if ($checkAdditional) {
|
---|
| 45 | if ($ownProperties) {
|
---|
| 46 | out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
|
---|
| 47 | } else {
|
---|
| 48 | out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
|
---|
| 49 | }
|
---|
| 50 | if ($someProperties) {
|
---|
| 51 | out += ' var isAdditional' + ($lvl) + ' = !(false ';
|
---|
| 52 | if ($schemaKeys.length) {
|
---|
| 53 | if ($schemaKeys.length > 8) {
|
---|
| 54 | out += ' || validate.schema' + ($schemaPath) + '.hasOwnProperty(' + ($key) + ') ';
|
---|
| 55 | } else {
|
---|
| 56 | var arr1 = $schemaKeys;
|
---|
| 57 | if (arr1) {
|
---|
| 58 | var $propertyKey, i1 = -1,
|
---|
| 59 | l1 = arr1.length - 1;
|
---|
| 60 | while (i1 < l1) {
|
---|
| 61 | $propertyKey = arr1[i1 += 1];
|
---|
| 62 | out += ' || ' + ($key) + ' == ' + (it.util.toQuotedString($propertyKey)) + ' ';
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | if ($pPropertyKeys.length) {
|
---|
| 68 | var arr2 = $pPropertyKeys;
|
---|
| 69 | if (arr2) {
|
---|
| 70 | var $pProperty, $i = -1,
|
---|
| 71 | l2 = arr2.length - 1;
|
---|
| 72 | while ($i < l2) {
|
---|
| 73 | $pProperty = arr2[$i += 1];
|
---|
| 74 | out += ' || ' + (it.usePattern($pProperty)) + '.test(' + ($key) + ') ';
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | out += ' ); if (isAdditional' + ($lvl) + ') { ';
|
---|
| 79 | }
|
---|
| 80 | if ($removeAdditional == 'all') {
|
---|
| 81 | out += ' delete ' + ($data) + '[' + ($key) + ']; ';
|
---|
| 82 | } else {
|
---|
| 83 | var $currentErrorPath = it.errorPath;
|
---|
| 84 | var $additionalProperty = '\' + ' + $key + ' + \'';
|
---|
| 85 | if (it.opts._errorDataPathProperty) {
|
---|
| 86 | it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
|
---|
| 87 | }
|
---|
| 88 | if ($noAdditional) {
|
---|
| 89 | if ($removeAdditional) {
|
---|
| 90 | out += ' delete ' + ($data) + '[' + ($key) + ']; ';
|
---|
| 91 | } else {
|
---|
| 92 | out += ' ' + ($nextValid) + ' = false; ';
|
---|
| 93 | var $currErrSchemaPath = $errSchemaPath;
|
---|
| 94 | $errSchemaPath = it.errSchemaPath + '/additionalProperties';
|
---|
| 95 | var $$outStack = $$outStack || [];
|
---|
| 96 | $$outStack.push(out);
|
---|
| 97 | out = ''; /* istanbul ignore else */
|
---|
| 98 | if (it.createErrors !== false) {
|
---|
| 99 | out += ' { keyword: \'' + ('additionalProperties') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { additionalProperty: \'' + ($additionalProperty) + '\' } ';
|
---|
| 100 | if (it.opts.messages !== false) {
|
---|
| 101 | out += ' , message: \'';
|
---|
| 102 | if (it.opts._errorDataPathProperty) {
|
---|
| 103 | out += 'is an invalid additional property';
|
---|
| 104 | } else {
|
---|
| 105 | out += 'should NOT have additional properties';
|
---|
| 106 | }
|
---|
| 107 | out += '\' ';
|
---|
| 108 | }
|
---|
| 109 | if (it.opts.verbose) {
|
---|
| 110 | out += ' , schema: false , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
|
---|
| 111 | }
|
---|
| 112 | out += ' } ';
|
---|
| 113 | } else {
|
---|
| 114 | out += ' {} ';
|
---|
| 115 | }
|
---|
| 116 | var __err = out;
|
---|
| 117 | out = $$outStack.pop();
|
---|
| 118 | if (!it.compositeRule && $breakOnError) {
|
---|
| 119 | /* istanbul ignore if */
|
---|
| 120 | if (it.async) {
|
---|
| 121 | out += ' throw new ValidationError([' + (__err) + ']); ';
|
---|
| 122 | } else {
|
---|
| 123 | out += ' validate.errors = [' + (__err) + ']; return false; ';
|
---|
| 124 | }
|
---|
| 125 | } else {
|
---|
| 126 | out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
|
---|
| 127 | }
|
---|
| 128 | $errSchemaPath = $currErrSchemaPath;
|
---|
| 129 | if ($breakOnError) {
|
---|
| 130 | out += ' break; ';
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | } else if ($additionalIsSchema) {
|
---|
| 134 | if ($removeAdditional == 'failing') {
|
---|
| 135 | out += ' var ' + ($errs) + ' = errors; ';
|
---|
| 136 | var $wasComposite = it.compositeRule;
|
---|
| 137 | it.compositeRule = $it.compositeRule = true;
|
---|
| 138 | $it.schema = $aProperties;
|
---|
| 139 | $it.schemaPath = it.schemaPath + '.additionalProperties';
|
---|
| 140 | $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
|
---|
| 141 | $it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
|
---|
| 142 | var $passData = $data + '[' + $key + ']';
|
---|
| 143 | $it.dataPathArr[$dataNxt] = $key;
|
---|
| 144 | var $code = it.validate($it);
|
---|
| 145 | $it.baseId = $currentBaseId;
|
---|
| 146 | if (it.util.varOccurences($code, $nextData) < 2) {
|
---|
| 147 | out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
|
---|
| 148 | } else {
|
---|
| 149 | out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
|
---|
| 150 | }
|
---|
| 151 | out += ' if (!' + ($nextValid) + ') { errors = ' + ($errs) + '; if (validate.errors !== null) { if (errors) validate.errors.length = errors; else validate.errors = null; } delete ' + ($data) + '[' + ($key) + ']; } ';
|
---|
| 152 | it.compositeRule = $it.compositeRule = $wasComposite;
|
---|
| 153 | } else {
|
---|
| 154 | $it.schema = $aProperties;
|
---|
| 155 | $it.schemaPath = it.schemaPath + '.additionalProperties';
|
---|
| 156 | $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
|
---|
| 157 | $it.errorPath = it.opts._errorDataPathProperty ? it.errorPath : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
|
---|
| 158 | var $passData = $data + '[' + $key + ']';
|
---|
| 159 | $it.dataPathArr[$dataNxt] = $key;
|
---|
| 160 | var $code = it.validate($it);
|
---|
| 161 | $it.baseId = $currentBaseId;
|
---|
| 162 | if (it.util.varOccurences($code, $nextData) < 2) {
|
---|
| 163 | out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
|
---|
| 164 | } else {
|
---|
| 165 | out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
|
---|
| 166 | }
|
---|
| 167 | if ($breakOnError) {
|
---|
| 168 | out += ' if (!' + ($nextValid) + ') break; ';
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | it.errorPath = $currentErrorPath;
|
---|
| 173 | }
|
---|
| 174 | if ($someProperties) {
|
---|
| 175 | out += ' } ';
|
---|
| 176 | }
|
---|
| 177 | out += ' } ';
|
---|
| 178 | if ($breakOnError) {
|
---|
| 179 | out += ' if (' + ($nextValid) + ') { ';
|
---|
| 180 | $closingBraces += '}';
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | var $useDefaults = it.opts.useDefaults && !it.compositeRule;
|
---|
| 184 | if ($schemaKeys.length) {
|
---|
| 185 | var arr3 = $schemaKeys;
|
---|
| 186 | if (arr3) {
|
---|
| 187 | var $propertyKey, i3 = -1,
|
---|
| 188 | l3 = arr3.length - 1;
|
---|
| 189 | while (i3 < l3) {
|
---|
| 190 | $propertyKey = arr3[i3 += 1];
|
---|
| 191 | var $sch = $schema[$propertyKey];
|
---|
| 192 | if ((it.opts.strictKeywords ? (typeof $sch == 'object' && Object.keys($sch).length > 0) || $sch === false : it.util.schemaHasRules($sch, it.RULES.all))) {
|
---|
| 193 | var $prop = it.util.getProperty($propertyKey),
|
---|
| 194 | $passData = $data + $prop,
|
---|
| 195 | $hasDefault = $useDefaults && $sch.default !== undefined;
|
---|
| 196 | $it.schema = $sch;
|
---|
| 197 | $it.schemaPath = $schemaPath + $prop;
|
---|
| 198 | $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($propertyKey);
|
---|
| 199 | $it.errorPath = it.util.getPath(it.errorPath, $propertyKey, it.opts.jsonPointers);
|
---|
| 200 | $it.dataPathArr[$dataNxt] = it.util.toQuotedString($propertyKey);
|
---|
| 201 | var $code = it.validate($it);
|
---|
| 202 | $it.baseId = $currentBaseId;
|
---|
| 203 | if (it.util.varOccurences($code, $nextData) < 2) {
|
---|
| 204 | $code = it.util.varReplace($code, $nextData, $passData);
|
---|
| 205 | var $useData = $passData;
|
---|
| 206 | } else {
|
---|
| 207 | var $useData = $nextData;
|
---|
| 208 | out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ';
|
---|
| 209 | }
|
---|
| 210 | if ($hasDefault) {
|
---|
| 211 | out += ' ' + ($code) + ' ';
|
---|
| 212 | } else {
|
---|
| 213 | if ($requiredHash && $requiredHash[$propertyKey]) {
|
---|
| 214 | out += ' if ( ' + ($useData) + ' === undefined ';
|
---|
| 215 | if ($ownProperties) {
|
---|
| 216 | out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
|
---|
| 217 | }
|
---|
| 218 | out += ') { ' + ($nextValid) + ' = false; ';
|
---|
| 219 | var $currentErrorPath = it.errorPath,
|
---|
| 220 | $currErrSchemaPath = $errSchemaPath,
|
---|
| 221 | $missingProperty = it.util.escapeQuotes($propertyKey);
|
---|
| 222 | if (it.opts._errorDataPathProperty) {
|
---|
| 223 | it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers);
|
---|
| 224 | }
|
---|
| 225 | $errSchemaPath = it.errSchemaPath + '/required';
|
---|
| 226 | var $$outStack = $$outStack || [];
|
---|
| 227 | $$outStack.push(out);
|
---|
| 228 | out = ''; /* istanbul ignore else */
|
---|
| 229 | if (it.createErrors !== false) {
|
---|
| 230 | out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';
|
---|
| 231 | if (it.opts.messages !== false) {
|
---|
| 232 | out += ' , message: \'';
|
---|
| 233 | if (it.opts._errorDataPathProperty) {
|
---|
| 234 | out += 'is a required property';
|
---|
| 235 | } else {
|
---|
| 236 | out += 'should have required property \\\'' + ($missingProperty) + '\\\'';
|
---|
| 237 | }
|
---|
| 238 | out += '\' ';
|
---|
| 239 | }
|
---|
| 240 | if (it.opts.verbose) {
|
---|
| 241 | out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
|
---|
| 242 | }
|
---|
| 243 | out += ' } ';
|
---|
| 244 | } else {
|
---|
| 245 | out += ' {} ';
|
---|
| 246 | }
|
---|
| 247 | var __err = out;
|
---|
| 248 | out = $$outStack.pop();
|
---|
| 249 | if (!it.compositeRule && $breakOnError) {
|
---|
| 250 | /* istanbul ignore if */
|
---|
| 251 | if (it.async) {
|
---|
| 252 | out += ' throw new ValidationError([' + (__err) + ']); ';
|
---|
| 253 | } else {
|
---|
| 254 | out += ' validate.errors = [' + (__err) + ']; return false; ';
|
---|
| 255 | }
|
---|
| 256 | } else {
|
---|
| 257 | out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
|
---|
| 258 | }
|
---|
| 259 | $errSchemaPath = $currErrSchemaPath;
|
---|
| 260 | it.errorPath = $currentErrorPath;
|
---|
| 261 | out += ' } else { ';
|
---|
| 262 | } else {
|
---|
| 263 | if ($breakOnError) {
|
---|
| 264 | out += ' if ( ' + ($useData) + ' === undefined ';
|
---|
| 265 | if ($ownProperties) {
|
---|
| 266 | out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
|
---|
| 267 | }
|
---|
| 268 | out += ') { ' + ($nextValid) + ' = true; } else { ';
|
---|
| 269 | } else {
|
---|
| 270 | out += ' if (' + ($useData) + ' !== undefined ';
|
---|
| 271 | if ($ownProperties) {
|
---|
| 272 | out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';
|
---|
| 273 | }
|
---|
| 274 | out += ' ) { ';
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 | out += ' ' + ($code) + ' } ';
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 | if ($breakOnError) {
|
---|
| 281 | out += ' if (' + ($nextValid) + ') { ';
|
---|
| 282 | $closingBraces += '}';
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 | if ($pPropertyKeys.length) {
|
---|
| 288 | var arr4 = $pPropertyKeys;
|
---|
| 289 | if (arr4) {
|
---|
| 290 | var $pProperty, i4 = -1,
|
---|
| 291 | l4 = arr4.length - 1;
|
---|
| 292 | while (i4 < l4) {
|
---|
| 293 | $pProperty = arr4[i4 += 1];
|
---|
| 294 | var $sch = $pProperties[$pProperty];
|
---|
| 295 | if ((it.opts.strictKeywords ? (typeof $sch == 'object' && Object.keys($sch).length > 0) || $sch === false : it.util.schemaHasRules($sch, it.RULES.all))) {
|
---|
| 296 | $it.schema = $sch;
|
---|
| 297 | $it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty);
|
---|
| 298 | $it.errSchemaPath = it.errSchemaPath + '/patternProperties/' + it.util.escapeFragment($pProperty);
|
---|
| 299 | if ($ownProperties) {
|
---|
| 300 | out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
|
---|
| 301 | } else {
|
---|
| 302 | out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
|
---|
| 303 | }
|
---|
| 304 | out += ' if (' + (it.usePattern($pProperty)) + '.test(' + ($key) + ')) { ';
|
---|
| 305 | $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
|
---|
| 306 | var $passData = $data + '[' + $key + ']';
|
---|
| 307 | $it.dataPathArr[$dataNxt] = $key;
|
---|
| 308 | var $code = it.validate($it);
|
---|
| 309 | $it.baseId = $currentBaseId;
|
---|
| 310 | if (it.util.varOccurences($code, $nextData) < 2) {
|
---|
| 311 | out += ' ' + (it.util.varReplace($code, $nextData, $passData)) + ' ';
|
---|
| 312 | } else {
|
---|
| 313 | out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
|
---|
| 314 | }
|
---|
| 315 | if ($breakOnError) {
|
---|
| 316 | out += ' if (!' + ($nextValid) + ') break; ';
|
---|
| 317 | }
|
---|
| 318 | out += ' } ';
|
---|
| 319 | if ($breakOnError) {
|
---|
| 320 | out += ' else ' + ($nextValid) + ' = true; ';
|
---|
| 321 | }
|
---|
| 322 | out += ' } ';
|
---|
| 323 | if ($breakOnError) {
|
---|
| 324 | out += ' if (' + ($nextValid) + ') { ';
|
---|
| 325 | $closingBraces += '}';
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 | if ($breakOnError) {
|
---|
| 332 | out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';
|
---|
| 333 | }
|
---|
| 334 | return out;
|
---|
| 335 | }
|
---|