Path: blob/master/node_modules/ajv/lib/dotjs/custom.js
1126 views
'use strict';1module.exports = function generate_custom(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 $errorKeyword;10var $data = 'data' + ($dataLvl || '');11var $valid = 'valid' + $lvl;12var $errs = 'errs__' + $lvl;13var $isData = it.opts.$data && $schema && $schema.$data,14$schemaValue;15if ($isData) {16out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';17$schemaValue = 'schema' + $lvl;18} else {19$schemaValue = $schema;20}21var $rule = this,22$definition = 'definition' + $lvl,23$rDef = $rule.definition,24$closingBraces = '';25var $compile, $inline, $macro, $ruleValidate, $validateCode;26if ($isData && $rDef.$data) {27$validateCode = 'keywordValidate' + $lvl;28var $validateSchema = $rDef.validateSchema;29out += ' var ' + ($definition) + ' = RULES.custom[\'' + ($keyword) + '\'].definition; var ' + ($validateCode) + ' = ' + ($definition) + '.validate;';30} else {31$ruleValidate = it.useCustomRule($rule, $schema, it.schema, it);32if (!$ruleValidate) return;33$schemaValue = 'validate.schema' + $schemaPath;34$validateCode = $ruleValidate.code;35$compile = $rDef.compile;36$inline = $rDef.inline;37$macro = $rDef.macro;38}39var $ruleErrs = $validateCode + '.errors',40$i = 'i' + $lvl,41$ruleErr = 'ruleErr' + $lvl,42$asyncKeyword = $rDef.async;43if ($asyncKeyword && !it.async) throw new Error('async keyword in sync schema');44if (!($inline || $macro)) {45out += '' + ($ruleErrs) + ' = null;';46}47out += 'var ' + ($errs) + ' = errors;var ' + ($valid) + ';';48if ($isData && $rDef.$data) {49$closingBraces += '}';50out += ' if (' + ($schemaValue) + ' === undefined) { ' + ($valid) + ' = true; } else { ';51if ($validateSchema) {52$closingBraces += '}';53out += ' ' + ($valid) + ' = ' + ($definition) + '.validateSchema(' + ($schemaValue) + '); if (' + ($valid) + ') { ';54}55}56if ($inline) {57if ($rDef.statements) {58out += ' ' + ($ruleValidate.validate) + ' ';59} else {60out += ' ' + ($valid) + ' = ' + ($ruleValidate.validate) + '; ';61}62} else if ($macro) {63var $it = it.util.copy(it);64var $closingBraces = '';65$it.level++;66var $nextValid = 'valid' + $it.level;67$it.schema = $ruleValidate.validate;68$it.schemaPath = '';69var $wasComposite = it.compositeRule;70it.compositeRule = $it.compositeRule = true;71var $code = it.validate($it).replace(/validate\.schema/g, $validateCode);72it.compositeRule = $it.compositeRule = $wasComposite;73out += ' ' + ($code);74} else {75var $$outStack = $$outStack || [];76$$outStack.push(out);77out = '';78out += ' ' + ($validateCode) + '.call( ';79if (it.opts.passContext) {80out += 'this';81} else {82out += 'self';83}84if ($compile || $rDef.schema === false) {85out += ' , ' + ($data) + ' ';86} else {87out += ' , ' + ($schemaValue) + ' , ' + ($data) + ' , validate.schema' + (it.schemaPath) + ' ';88}89out += ' , (dataPath || \'\')';90if (it.errorPath != '""') {91out += ' + ' + (it.errorPath);92}93var $parentData = $dataLvl ? 'data' + (($dataLvl - 1) || '') : 'parentData',94$parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';95out += ' , ' + ($parentData) + ' , ' + ($parentDataProperty) + ' , rootData ) ';96var def_callRuleValidate = out;97out = $$outStack.pop();98if ($rDef.errors === false) {99out += ' ' + ($valid) + ' = ';100if ($asyncKeyword) {101out += 'await ';102}103out += '' + (def_callRuleValidate) + '; ';104} else {105if ($asyncKeyword) {106$ruleErrs = 'customErrors' + $lvl;107out += ' var ' + ($ruleErrs) + ' = null; try { ' + ($valid) + ' = await ' + (def_callRuleValidate) + '; } catch (e) { ' + ($valid) + ' = false; if (e instanceof ValidationError) ' + ($ruleErrs) + ' = e.errors; else throw e; } ';108} else {109out += ' ' + ($ruleErrs) + ' = null; ' + ($valid) + ' = ' + (def_callRuleValidate) + '; ';110}111}112}113if ($rDef.modifying) {114out += ' if (' + ($parentData) + ') ' + ($data) + ' = ' + ($parentData) + '[' + ($parentDataProperty) + '];';115}116out += '' + ($closingBraces);117if ($rDef.valid) {118if ($breakOnError) {119out += ' if (true) { ';120}121} else {122out += ' if ( ';123if ($rDef.valid === undefined) {124out += ' !';125if ($macro) {126out += '' + ($nextValid);127} else {128out += '' + ($valid);129}130} else {131out += ' ' + (!$rDef.valid) + ' ';132}133out += ') { ';134$errorKeyword = $rule.keyword;135var $$outStack = $$outStack || [];136$$outStack.push(out);137out = '';138var $$outStack = $$outStack || [];139$$outStack.push(out);140out = ''; /* istanbul ignore else */141if (it.createErrors !== false) {142out += ' { keyword: \'' + ($errorKeyword || 'custom') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { keyword: \'' + ($rule.keyword) + '\' } ';143if (it.opts.messages !== false) {144out += ' , message: \'should pass "' + ($rule.keyword) + '" keyword validation\' ';145}146if (it.opts.verbose) {147out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';148}149out += ' } ';150} else {151out += ' {} ';152}153var __err = out;154out = $$outStack.pop();155if (!it.compositeRule && $breakOnError) {156/* istanbul ignore if */157if (it.async) {158out += ' throw new ValidationError([' + (__err) + ']); ';159} else {160out += ' validate.errors = [' + (__err) + ']; return false; ';161}162} else {163out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';164}165var def_customError = out;166out = $$outStack.pop();167if ($inline) {168if ($rDef.errors) {169if ($rDef.errors != 'full') {170out += ' for (var ' + ($i) + '=' + ($errs) + '; ' + ($i) + '<errors; ' + ($i) + '++) { var ' + ($ruleErr) + ' = vErrors[' + ($i) + ']; if (' + ($ruleErr) + '.dataPath === undefined) ' + ($ruleErr) + '.dataPath = (dataPath || \'\') + ' + (it.errorPath) + '; if (' + ($ruleErr) + '.schemaPath === undefined) { ' + ($ruleErr) + '.schemaPath = "' + ($errSchemaPath) + '"; } ';171if (it.opts.verbose) {172out += ' ' + ($ruleErr) + '.schema = ' + ($schemaValue) + '; ' + ($ruleErr) + '.data = ' + ($data) + '; ';173}174out += ' } ';175}176} else {177if ($rDef.errors === false) {178out += ' ' + (def_customError) + ' ';179} else {180out += ' if (' + ($errs) + ' == errors) { ' + (def_customError) + ' } else { for (var ' + ($i) + '=' + ($errs) + '; ' + ($i) + '<errors; ' + ($i) + '++) { var ' + ($ruleErr) + ' = vErrors[' + ($i) + ']; if (' + ($ruleErr) + '.dataPath === undefined) ' + ($ruleErr) + '.dataPath = (dataPath || \'\') + ' + (it.errorPath) + '; if (' + ($ruleErr) + '.schemaPath === undefined) { ' + ($ruleErr) + '.schemaPath = "' + ($errSchemaPath) + '"; } ';181if (it.opts.verbose) {182out += ' ' + ($ruleErr) + '.schema = ' + ($schemaValue) + '; ' + ($ruleErr) + '.data = ' + ($data) + '; ';183}184out += ' } } ';185}186}187} else if ($macro) {188out += ' var err = '; /* istanbul ignore else */189if (it.createErrors !== false) {190out += ' { keyword: \'' + ($errorKeyword || 'custom') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { keyword: \'' + ($rule.keyword) + '\' } ';191if (it.opts.messages !== false) {192out += ' , message: \'should pass "' + ($rule.keyword) + '" keyword validation\' ';193}194if (it.opts.verbose) {195out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';196}197out += ' } ';198} else {199out += ' {} ';200}201out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';202if (!it.compositeRule && $breakOnError) {203/* istanbul ignore if */204if (it.async) {205out += ' throw new ValidationError(vErrors); ';206} else {207out += ' validate.errors = vErrors; return false; ';208}209}210} else {211if ($rDef.errors === false) {212out += ' ' + (def_customError) + ' ';213} else {214out += ' if (Array.isArray(' + ($ruleErrs) + ')) { if (vErrors === null) vErrors = ' + ($ruleErrs) + '; else vErrors = vErrors.concat(' + ($ruleErrs) + '); errors = vErrors.length; for (var ' + ($i) + '=' + ($errs) + '; ' + ($i) + '<errors; ' + ($i) + '++) { var ' + ($ruleErr) + ' = vErrors[' + ($i) + ']; if (' + ($ruleErr) + '.dataPath === undefined) ' + ($ruleErr) + '.dataPath = (dataPath || \'\') + ' + (it.errorPath) + '; ' + ($ruleErr) + '.schemaPath = "' + ($errSchemaPath) + '"; ';215if (it.opts.verbose) {216out += ' ' + ($ruleErr) + '.schema = ' + ($schemaValue) + '; ' + ($ruleErr) + '.data = ' + ($data) + '; ';217}218out += ' } } else { ' + (def_customError) + ' } ';219}220}221out += ' } ';222if ($breakOnError) {223out += ' else { ';224}225}226return out;227}228229230