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