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