Path: blob/master/node_modules/ajv/lib/dotjs/dependencies.js
1126 views
'use strict';1module.exports = function generate_dependencies(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 $schemaDeps = {},16$propertyDeps = {},17$ownProperties = it.opts.ownProperties;18for ($property in $schema) {19if ($property == '__proto__') continue;20var $sch = $schema[$property];21var $deps = Array.isArray($sch) ? $propertyDeps : $schemaDeps;22$deps[$property] = $sch;23}24out += 'var ' + ($errs) + ' = errors;';25var $currentErrorPath = it.errorPath;26out += 'var missing' + ($lvl) + ';';27for (var $property in $propertyDeps) {28$deps = $propertyDeps[$property];29if ($deps.length) {30out += ' if ( ' + ($data) + (it.util.getProperty($property)) + ' !== undefined ';31if ($ownProperties) {32out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($property)) + '\') ';33}34if ($breakOnError) {35out += ' && ( ';36var arr1 = $deps;37if (arr1) {38var $propertyKey, $i = -1,39l1 = arr1.length - 1;40while ($i < l1) {41$propertyKey = arr1[$i += 1];42if ($i) {43out += ' || ';44}45var $prop = it.util.getProperty($propertyKey),46$useData = $data + $prop;47out += ' ( ( ' + ($useData) + ' === undefined ';48if ($ownProperties) {49out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';50}51out += ') && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? $propertyKey : $prop)) + ') ) ';52}53}54out += ')) { ';55var $propertyPath = 'missing' + $lvl,56$missingProperty = '\' + ' + $propertyPath + ' + \'';57if (it.opts._errorDataPathProperty) {58it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath;59}60var $$outStack = $$outStack || [];61$$outStack.push(out);62out = ''; /* istanbul ignore else */63if (it.createErrors !== false) {64out += ' { 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(", "))) + '\' } ';65if (it.opts.messages !== false) {66out += ' , message: \'should have ';67if ($deps.length == 1) {68out += 'property ' + (it.util.escapeQuotes($deps[0]));69} else {70out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));71}72out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';73}74if (it.opts.verbose) {75out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';76}77out += ' } ';78} else {79out += ' {} ';80}81var __err = out;82out = $$outStack.pop();83if (!it.compositeRule && $breakOnError) {84/* istanbul ignore if */85if (it.async) {86out += ' throw new ValidationError([' + (__err) + ']); ';87} else {88out += ' validate.errors = [' + (__err) + ']; return false; ';89}90} else {91out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';92}93} else {94out += ' ) { ';95var arr2 = $deps;96if (arr2) {97var $propertyKey, i2 = -1,98l2 = arr2.length - 1;99while (i2 < l2) {100$propertyKey = arr2[i2 += 1];101var $prop = it.util.getProperty($propertyKey),102$missingProperty = it.util.escapeQuotes($propertyKey),103$useData = $data + $prop;104if (it.opts._errorDataPathProperty) {105it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers);106}107out += ' if ( ' + ($useData) + ' === undefined ';108if ($ownProperties) {109out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';110}111out += ') { var err = '; /* istanbul ignore else */112if (it.createErrors !== false) {113out += ' { 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(", "))) + '\' } ';114if (it.opts.messages !== false) {115out += ' , message: \'should have ';116if ($deps.length == 1) {117out += 'property ' + (it.util.escapeQuotes($deps[0]));118} else {119out += 'properties ' + (it.util.escapeQuotes($deps.join(", ")));120}121out += ' when property ' + (it.util.escapeQuotes($property)) + ' is present\' ';122}123if (it.opts.verbose) {124out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';125}126out += ' } ';127} else {128out += ' {} ';129}130out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';131}132}133}134out += ' } ';135if ($breakOnError) {136$closingBraces += '}';137out += ' else { ';138}139}140}141it.errorPath = $currentErrorPath;142var $currentBaseId = $it.baseId;143for (var $property in $schemaDeps) {144var $sch = $schemaDeps[$property];145if ((it.opts.strictKeywords ? (typeof $sch == 'object' && Object.keys($sch).length > 0) || $sch === false : it.util.schemaHasRules($sch, it.RULES.all))) {146out += ' ' + ($nextValid) + ' = true; if ( ' + ($data) + (it.util.getProperty($property)) + ' !== undefined ';147if ($ownProperties) {148out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($property)) + '\') ';149}150out += ') { ';151$it.schema = $sch;152$it.schemaPath = $schemaPath + it.util.getProperty($property);153$it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($property);154out += ' ' + (it.validate($it)) + ' ';155$it.baseId = $currentBaseId;156out += ' } ';157if ($breakOnError) {158out += ' if (' + ($nextValid) + ') { ';159$closingBraces += '}';160}161}162}163if ($breakOnError) {164out += ' ' + ($closingBraces) + ' if (' + ($errs) + ' == errors) {';165}166return out;167}168169170