Path: blob/master/node_modules/ajv/lib/dotjs/required.js
1126 views
'use strict';1module.exports = function generate_required(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 $valid = 'valid' + $lvl;11var $isData = it.opts.$data && $schema && $schema.$data,12$schemaValue;13if ($isData) {14out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';15$schemaValue = 'schema' + $lvl;16} else {17$schemaValue = $schema;18}19var $vSchema = 'schema' + $lvl;20if (!$isData) {21if ($schema.length < it.opts.loopRequired && it.schema.properties && Object.keys(it.schema.properties).length) {22var $required = [];23var arr1 = $schema;24if (arr1) {25var $property, i1 = -1,26l1 = arr1.length - 1;27while (i1 < l1) {28$property = arr1[i1 += 1];29var $propertySch = it.schema.properties[$property];30if (!($propertySch && (it.opts.strictKeywords ? (typeof $propertySch == 'object' && Object.keys($propertySch).length > 0) || $propertySch === false : it.util.schemaHasRules($propertySch, it.RULES.all)))) {31$required[$required.length] = $property;32}33}34}35} else {36var $required = $schema;37}38}39if ($isData || $required.length) {40var $currentErrorPath = it.errorPath,41$loopRequired = $isData || $required.length >= it.opts.loopRequired,42$ownProperties = it.opts.ownProperties;43if ($breakOnError) {44out += ' var missing' + ($lvl) + '; ';45if ($loopRequired) {46if (!$isData) {47out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + '; ';48}49var $i = 'i' + $lvl,50$propertyPath = 'schema' + $lvl + '[' + $i + ']',51$missingProperty = '\' + ' + $propertyPath + ' + \'';52if (it.opts._errorDataPathProperty) {53it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);54}55out += ' var ' + ($valid) + ' = true; ';56if ($isData) {57out += ' if (schema' + ($lvl) + ' === undefined) ' + ($valid) + ' = true; else if (!Array.isArray(schema' + ($lvl) + ')) ' + ($valid) + ' = false; else {';58}59out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { ' + ($valid) + ' = ' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] !== undefined ';60if ($ownProperties) {61out += ' && Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($vSchema) + '[' + ($i) + ']) ';62}63out += '; if (!' + ($valid) + ') break; } ';64if ($isData) {65out += ' } ';66}67out += ' if (!' + ($valid) + ') { ';68var $$outStack = $$outStack || [];69$$outStack.push(out);70out = ''; /* istanbul ignore else */71if (it.createErrors !== false) {72out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';73if (it.opts.messages !== false) {74out += ' , message: \'';75if (it.opts._errorDataPathProperty) {76out += 'is a required property';77} else {78out += 'should have required property \\\'' + ($missingProperty) + '\\\'';79}80out += '\' ';81}82if (it.opts.verbose) {83out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';84}85out += ' } ';86} else {87out += ' {} ';88}89var __err = out;90out = $$outStack.pop();91if (!it.compositeRule && $breakOnError) {92/* istanbul ignore if */93if (it.async) {94out += ' throw new ValidationError([' + (__err) + ']); ';95} else {96out += ' validate.errors = [' + (__err) + ']; return false; ';97}98} else {99out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';100}101out += ' } else { ';102} else {103out += ' if ( ';104var arr2 = $required;105if (arr2) {106var $propertyKey, $i = -1,107l2 = arr2.length - 1;108while ($i < l2) {109$propertyKey = arr2[$i += 1];110if ($i) {111out += ' || ';112}113var $prop = it.util.getProperty($propertyKey),114$useData = $data + $prop;115out += ' ( ( ' + ($useData) + ' === undefined ';116if ($ownProperties) {117out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';118}119out += ') && (missing' + ($lvl) + ' = ' + (it.util.toQuotedString(it.opts.jsonPointers ? $propertyKey : $prop)) + ') ) ';120}121}122out += ') { ';123var $propertyPath = 'missing' + $lvl,124$missingProperty = '\' + ' + $propertyPath + ' + \'';125if (it.opts._errorDataPathProperty) {126it.errorPath = it.opts.jsonPointers ? it.util.getPathExpr($currentErrorPath, $propertyPath, true) : $currentErrorPath + ' + ' + $propertyPath;127}128var $$outStack = $$outStack || [];129$$outStack.push(out);130out = ''; /* istanbul ignore else */131if (it.createErrors !== false) {132out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';133if (it.opts.messages !== false) {134out += ' , message: \'';135if (it.opts._errorDataPathProperty) {136out += 'is a required property';137} else {138out += 'should have required property \\\'' + ($missingProperty) + '\\\'';139}140out += '\' ';141}142if (it.opts.verbose) {143out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';144}145out += ' } ';146} else {147out += ' {} ';148}149var __err = out;150out = $$outStack.pop();151if (!it.compositeRule && $breakOnError) {152/* istanbul ignore if */153if (it.async) {154out += ' throw new ValidationError([' + (__err) + ']); ';155} else {156out += ' validate.errors = [' + (__err) + ']; return false; ';157}158} else {159out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';160}161out += ' } else { ';162}163} else {164if ($loopRequired) {165if (!$isData) {166out += ' var ' + ($vSchema) + ' = validate.schema' + ($schemaPath) + '; ';167}168var $i = 'i' + $lvl,169$propertyPath = 'schema' + $lvl + '[' + $i + ']',170$missingProperty = '\' + ' + $propertyPath + ' + \'';171if (it.opts._errorDataPathProperty) {172it.errorPath = it.util.getPathExpr($currentErrorPath, $propertyPath, it.opts.jsonPointers);173}174if ($isData) {175out += ' if (' + ($vSchema) + ' && !Array.isArray(' + ($vSchema) + ')) { var err = '; /* istanbul ignore else */176if (it.createErrors !== false) {177out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';178if (it.opts.messages !== false) {179out += ' , message: \'';180if (it.opts._errorDataPathProperty) {181out += 'is a required property';182} else {183out += 'should have required property \\\'' + ($missingProperty) + '\\\'';184}185out += '\' ';186}187if (it.opts.verbose) {188out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';189}190out += ' } ';191} else {192out += ' {} ';193}194out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else if (' + ($vSchema) + ' !== undefined) { ';195}196out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < ' + ($vSchema) + '.length; ' + ($i) + '++) { if (' + ($data) + '[' + ($vSchema) + '[' + ($i) + ']] === undefined ';197if ($ownProperties) {198out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', ' + ($vSchema) + '[' + ($i) + ']) ';199}200out += ') { var err = '; /* istanbul ignore else */201if (it.createErrors !== false) {202out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';203if (it.opts.messages !== false) {204out += ' , message: \'';205if (it.opts._errorDataPathProperty) {206out += 'is a required property';207} else {208out += 'should have required property \\\'' + ($missingProperty) + '\\\'';209}210out += '\' ';211}212if (it.opts.verbose) {213out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';214}215out += ' } ';216} else {217out += ' {} ';218}219out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } } ';220if ($isData) {221out += ' } ';222}223} else {224var arr3 = $required;225if (arr3) {226var $propertyKey, i3 = -1,227l3 = arr3.length - 1;228while (i3 < l3) {229$propertyKey = arr3[i3 += 1];230var $prop = it.util.getProperty($propertyKey),231$missingProperty = it.util.escapeQuotes($propertyKey),232$useData = $data + $prop;233if (it.opts._errorDataPathProperty) {234it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers);235}236out += ' if ( ' + ($useData) + ' === undefined ';237if ($ownProperties) {238out += ' || ! Object.prototype.hasOwnProperty.call(' + ($data) + ', \'' + (it.util.escapeQuotes($propertyKey)) + '\') ';239}240out += ') { var err = '; /* istanbul ignore else */241if (it.createErrors !== false) {242out += ' { keyword: \'' + ('required') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingProperty: \'' + ($missingProperty) + '\' } ';243if (it.opts.messages !== false) {244out += ' , message: \'';245if (it.opts._errorDataPathProperty) {246out += 'is a required property';247} else {248out += 'should have required property \\\'' + ($missingProperty) + '\\\'';249}250out += '\' ';251}252if (it.opts.verbose) {253out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';254}255out += ' } ';256} else {257out += ' {} ';258}259out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';260}261}262}263}264it.errorPath = $currentErrorPath;265} else if ($breakOnError) {266out += ' if (true) {';267}268return out;269}270271272