Path: blob/master/node_modules/ajv/lib/dotjs/oneOf.js
2593 views
'use strict';1module.exports = function generate_oneOf(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 $errs = 'errs__' + $lvl;12var $it = it.util.copy(it);13var $closingBraces = '';14$it.level++;15var $nextValid = 'valid' + $it.level;16var $currentBaseId = $it.baseId,17$prevValid = 'prevValid' + $lvl,18$passingSchemas = 'passingSchemas' + $lvl;19out += 'var ' + ($errs) + ' = errors , ' + ($prevValid) + ' = false , ' + ($valid) + ' = false , ' + ($passingSchemas) + ' = null; ';20var $wasComposite = it.compositeRule;21it.compositeRule = $it.compositeRule = true;22var arr1 = $schema;23if (arr1) {24var $sch, $i = -1,25l1 = arr1.length - 1;26while ($i < l1) {27$sch = arr1[$i += 1];28if ((it.opts.strictKeywords ? (typeof $sch == 'object' && Object.keys($sch).length > 0) || $sch === false : it.util.schemaHasRules($sch, it.RULES.all))) {29$it.schema = $sch;30$it.schemaPath = $schemaPath + '[' + $i + ']';31$it.errSchemaPath = $errSchemaPath + '/' + $i;32out += ' ' + (it.validate($it)) + ' ';33$it.baseId = $currentBaseId;34} else {35out += ' var ' + ($nextValid) + ' = true; ';36}37if ($i) {38out += ' if (' + ($nextValid) + ' && ' + ($prevValid) + ') { ' + ($valid) + ' = false; ' + ($passingSchemas) + ' = [' + ($passingSchemas) + ', ' + ($i) + ']; } else { ';39$closingBraces += '}';40}41out += ' if (' + ($nextValid) + ') { ' + ($valid) + ' = ' + ($prevValid) + ' = true; ' + ($passingSchemas) + ' = ' + ($i) + '; }';42}43}44it.compositeRule = $it.compositeRule = $wasComposite;45out += '' + ($closingBraces) + 'if (!' + ($valid) + ') { var err = '; /* istanbul ignore else */46if (it.createErrors !== false) {47out += ' { keyword: \'' + ('oneOf') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { passingSchemas: ' + ($passingSchemas) + ' } ';48if (it.opts.messages !== false) {49out += ' , message: \'should match exactly one schema in oneOf\' ';50}51if (it.opts.verbose) {52out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';53}54out += ' } ';55} else {56out += ' {} ';57}58out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';59if (!it.compositeRule && $breakOnError) {60/* istanbul ignore if */61if (it.async) {62out += ' throw new ValidationError(vErrors); ';63} else {64out += ' validate.errors = vErrors; return false; ';65}66}67out += '} else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; }';68if (it.opts.allErrors) {69out += ' } ';70}71return out;72}737475