2 module.exports = function generate_ref(it, $keyword, $ruleType) {
5 var $dataLvl = it.dataLevel;
6 var $schema = it.schema[$keyword];
7 var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
8 var $breakOnError = !it.opts.allErrors;
9 var $data = 'data' + ($dataLvl || '');
10 var $valid = 'valid' + $lvl;
12 if ($schema == '#' || $schema == '#/') {
15 $refCode = 'validate';
17 $async = it.root.schema.$async === true;
18 $refCode = 'root.refVal[0]';
21 var $refVal = it.resolveRef(it.baseId, $schema, it.isRoot);
22 if ($refVal === undefined) {
23 var $message = it.MissingRefError.message(it.baseId, $schema);
24 if (it.opts.missingRefs == 'fail') {
25 it.logger.error($message);
26 var $$outStack = $$outStack || [];
28 out = ''; /* istanbul ignore else */
29 if (it.createErrors !== false) {
30 out += ' { keyword: \'' + ('$ref') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { ref: \'' + (it.util.escapeQuotes($schema)) + '\' } ';
31 if (it.opts.messages !== false) {
32 out += ' , message: \'can\\\'t resolve reference ' + (it.util.escapeQuotes($schema)) + '\' ';
34 if (it.opts.verbose) {
35 out += ' , schema: ' + (it.util.toQuotedString($schema)) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
42 out = $$outStack.pop();
43 if (!it.compositeRule && $breakOnError) {
44 /* istanbul ignore if */
46 out += ' throw new ValidationError([' + (__err) + ']); ';
48 out += ' validate.errors = [' + (__err) + ']; return false; ';
51 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
54 out += ' if (false) { ';
56 } else if (it.opts.missingRefs == 'ignore') {
57 it.logger.warn($message);
59 out += ' if (true) { ';
62 throw new it.MissingRefError(it.baseId, $schema, $message);
64 } else if ($refVal.inline) {
65 var $it = it.util.copy(it);
67 var $nextValid = 'valid' + $it.level;
68 $it.schema = $refVal.schema;
70 $it.errSchemaPath = $schema;
71 var $code = it.validate($it).replace(/validate\.schema/g, $refVal.code);
72 out += ' ' + ($code) + ' ';
74 out += ' if (' + ($nextValid) + ') { ';
77 $async = $refVal.$async === true || (it.async && $refVal.$async !== false);
78 $refCode = $refVal.code;
82 var $$outStack = $$outStack || [];
85 if (it.opts.passContext) {
86 out += ' ' + ($refCode) + '.call(this, ';
88 out += ' ' + ($refCode) + '( ';
90 out += ' ' + ($data) + ', (dataPath || \'\')';
91 if (it.errorPath != '""') {
92 out += ' + ' + (it.errorPath);
94 var $parentData = $dataLvl ? 'data' + (($dataLvl - 1) || '') : 'parentData',
95 $parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
96 out += ' , ' + ($parentData) + ' , ' + ($parentDataProperty) + ', rootData) ';
97 var __callValidate = out;
98 out = $$outStack.pop();
100 if (!it.async) throw new Error('async schema referenced by sync schema');
102 out += ' var ' + ($valid) + '; ';
104 out += ' try { await ' + (__callValidate) + '; ';
106 out += ' ' + ($valid) + ' = true; ';
108 out += ' } catch (e) { if (!(e instanceof ValidationError)) throw e; if (vErrors === null) vErrors = e.errors; else vErrors = vErrors.concat(e.errors); errors = vErrors.length; ';
110 out += ' ' + ($valid) + ' = false; ';
114 out += ' if (' + ($valid) + ') { ';
117 out += ' if (!' + (__callValidate) + ') { if (vErrors === null) vErrors = ' + ($refCode) + '.errors; else vErrors = vErrors.concat(' + ($refCode) + '.errors); errors = vErrors.length; } ';