Path: blob/master/node_modules/ajv/lib/dotjs/ref.js
2593 views
'use strict';1module.exports = function generate_ref(it, $keyword, $ruleType) {2var out = ' ';3var $lvl = it.level;4var $dataLvl = it.dataLevel;5var $schema = it.schema[$keyword];6var $errSchemaPath = it.errSchemaPath + '/' + $keyword;7var $breakOnError = !it.opts.allErrors;8var $data = 'data' + ($dataLvl || '');9var $valid = 'valid' + $lvl;10var $async, $refCode;11if ($schema == '#' || $schema == '#/') {12if (it.isRoot) {13$async = it.async;14$refCode = 'validate';15} else {16$async = it.root.schema.$async === true;17$refCode = 'root.refVal[0]';18}19} else {20var $refVal = it.resolveRef(it.baseId, $schema, it.isRoot);21if ($refVal === undefined) {22var $message = it.MissingRefError.message(it.baseId, $schema);23if (it.opts.missingRefs == 'fail') {24it.logger.error($message);25var $$outStack = $$outStack || [];26$$outStack.push(out);27out = ''; /* istanbul ignore else */28if (it.createErrors !== false) {29out += ' { keyword: \'' + ('$ref') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { ref: \'' + (it.util.escapeQuotes($schema)) + '\' } ';30if (it.opts.messages !== false) {31out += ' , message: \'can\\\'t resolve reference ' + (it.util.escapeQuotes($schema)) + '\' ';32}33if (it.opts.verbose) {34out += ' , schema: ' + (it.util.toQuotedString($schema)) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';35}36out += ' } ';37} else {38out += ' {} ';39}40var __err = out;41out = $$outStack.pop();42if (!it.compositeRule && $breakOnError) {43/* istanbul ignore if */44if (it.async) {45out += ' throw new ValidationError([' + (__err) + ']); ';46} else {47out += ' validate.errors = [' + (__err) + ']; return false; ';48}49} else {50out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';51}52if ($breakOnError) {53out += ' if (false) { ';54}55} else if (it.opts.missingRefs == 'ignore') {56it.logger.warn($message);57if ($breakOnError) {58out += ' if (true) { ';59}60} else {61throw new it.MissingRefError(it.baseId, $schema, $message);62}63} else if ($refVal.inline) {64var $it = it.util.copy(it);65$it.level++;66var $nextValid = 'valid' + $it.level;67$it.schema = $refVal.schema;68$it.schemaPath = '';69$it.errSchemaPath = $schema;70var $code = it.validate($it).replace(/validate\.schema/g, $refVal.code);71out += ' ' + ($code) + ' ';72if ($breakOnError) {73out += ' if (' + ($nextValid) + ') { ';74}75} else {76$async = $refVal.$async === true || (it.async && $refVal.$async !== false);77$refCode = $refVal.code;78}79}80if ($refCode) {81var $$outStack = $$outStack || [];82$$outStack.push(out);83out = '';84if (it.opts.passContext) {85out += ' ' + ($refCode) + '.call(this, ';86} else {87out += ' ' + ($refCode) + '( ';88}89out += ' ' + ($data) + ', (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 __callValidate = out;97out = $$outStack.pop();98if ($async) {99if (!it.async) throw new Error('async schema referenced by sync schema');100if ($breakOnError) {101out += ' var ' + ($valid) + '; ';102}103out += ' try { await ' + (__callValidate) + '; ';104if ($breakOnError) {105out += ' ' + ($valid) + ' = true; ';106}107out += ' } catch (e) { if (!(e instanceof ValidationError)) throw e; if (vErrors === null) vErrors = e.errors; else vErrors = vErrors.concat(e.errors); errors = vErrors.length; ';108if ($breakOnError) {109out += ' ' + ($valid) + ' = false; ';110}111out += ' } ';112if ($breakOnError) {113out += ' if (' + ($valid) + ') { ';114}115} else {116out += ' if (!' + (__callValidate) + ') { if (vErrors === null) vErrors = ' + ($refCode) + '.errors; else vErrors = vErrors.concat(' + ($refCode) + '.errors); errors = vErrors.length; } ';117if ($breakOnError) {118out += ' else { ';119}120}121}122return out;123}124125126