Path: blob/master/node_modules/ajv/lib/dotjs/if.js
2593 views
'use strict';1module.exports = function generate_if(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);13$it.level++;14var $nextValid = 'valid' + $it.level;15var $thenSch = it.schema['then'],16$elseSch = it.schema['else'],17$thenPresent = $thenSch !== undefined && (it.opts.strictKeywords ? (typeof $thenSch == 'object' && Object.keys($thenSch).length > 0) || $thenSch === false : it.util.schemaHasRules($thenSch, it.RULES.all)),18$elsePresent = $elseSch !== undefined && (it.opts.strictKeywords ? (typeof $elseSch == 'object' && Object.keys($elseSch).length > 0) || $elseSch === false : it.util.schemaHasRules($elseSch, it.RULES.all)),19$currentBaseId = $it.baseId;20if ($thenPresent || $elsePresent) {21var $ifClause;22$it.createErrors = false;23$it.schema = $schema;24$it.schemaPath = $schemaPath;25$it.errSchemaPath = $errSchemaPath;26out += ' var ' + ($errs) + ' = errors; var ' + ($valid) + ' = true; ';27var $wasComposite = it.compositeRule;28it.compositeRule = $it.compositeRule = true;29out += ' ' + (it.validate($it)) + ' ';30$it.baseId = $currentBaseId;31$it.createErrors = true;32out += ' errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } ';33it.compositeRule = $it.compositeRule = $wasComposite;34if ($thenPresent) {35out += ' if (' + ($nextValid) + ') { ';36$it.schema = it.schema['then'];37$it.schemaPath = it.schemaPath + '.then';38$it.errSchemaPath = it.errSchemaPath + '/then';39out += ' ' + (it.validate($it)) + ' ';40$it.baseId = $currentBaseId;41out += ' ' + ($valid) + ' = ' + ($nextValid) + '; ';42if ($thenPresent && $elsePresent) {43$ifClause = 'ifClause' + $lvl;44out += ' var ' + ($ifClause) + ' = \'then\'; ';45} else {46$ifClause = '\'then\'';47}48out += ' } ';49if ($elsePresent) {50out += ' else { ';51}52} else {53out += ' if (!' + ($nextValid) + ') { ';54}55if ($elsePresent) {56$it.schema = it.schema['else'];57$it.schemaPath = it.schemaPath + '.else';58$it.errSchemaPath = it.errSchemaPath + '/else';59out += ' ' + (it.validate($it)) + ' ';60$it.baseId = $currentBaseId;61out += ' ' + ($valid) + ' = ' + ($nextValid) + '; ';62if ($thenPresent && $elsePresent) {63$ifClause = 'ifClause' + $lvl;64out += ' var ' + ($ifClause) + ' = \'else\'; ';65} else {66$ifClause = '\'else\'';67}68out += ' } ';69}70out += ' if (!' + ($valid) + ') { var err = '; /* istanbul ignore else */71if (it.createErrors !== false) {72out += ' { keyword: \'' + ('if') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { failingKeyword: ' + ($ifClause) + ' } ';73if (it.opts.messages !== false) {74out += ' , message: \'should match "\' + ' + ($ifClause) + ' + \'" schema\' ';75}76if (it.opts.verbose) {77out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';78}79out += ' } ';80} else {81out += ' {} ';82}83out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';84if (!it.compositeRule && $breakOnError) {85/* istanbul ignore if */86if (it.async) {87out += ' throw new ValidationError(vErrors); ';88} else {89out += ' validate.errors = vErrors; return false; ';90}91}92out += ' } ';93if ($breakOnError) {94out += ' else { ';95}96} else {97if ($breakOnError) {98out += ' if (true) { ';99}100}101return out;102}103104105