2 module.exports = function generate_ref(it, $keyword) {
5 var $dataLvl = it.dataLevel;
6 var $schema = it.schema[$keyword];
7 var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
8 var $breakOnError = !it.opts.allErrors;
10 var $data = 'data' + ($dataLvl || '');
11 var $valid = 'valid' + $lvl;
13 if ($schema == '#' || $schema == '#/') {
16 $refCode = 'validate';
18 $async = it.root.schema.$async === true;
19 $refCode = 'root.refVal[0]';
22 var $refVal = it.resolveRef(it.baseId, $schema, it.isRoot);
23 if ($refVal === undefined) {
24 var $message = 'can\'t resolve reference ' + $schema + ' from id ' + it.baseId;
25 if (it.opts.missingRefs == 'fail') {
26 console.log($message);
27 var $$outStack = $$outStack || [];
29 out = ''; /* istanbul ignore else */
30 if (it.createErrors !== false) {
31 out += ' { keyword: \'' + ($errorKeyword || '$ref') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { ref: \'' + (it.util.escapeQuotes($schema)) + '\' } ';
32 if (it.opts.messages !== false) {
33 out += ' , message: \'can\\\'t resolve reference ' + (it.util.escapeQuotes($schema)) + '\' ';
35 if (it.opts.verbose) {
36 out += ' , schema: ' + (it.util.toQuotedString($schema)) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
43 out = $$outStack.pop();
44 if (!it.compositeRule && $breakOnError) { /* 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 console.log($message);
59 out += ' if (true) { ';
62 var $error = new Error($message);
63 $error.missingRef = it.resolve.url(it.baseId, $schema);
64 $error.missingSchema = it.resolve.normalizeId(it.resolve.fullPath($error.missingRef));
67 } else if ($refVal.inline) {
68 var $it = it.util.copy(it);
70 var $nextValid = 'valid' + $it.level;
71 $it.schema = $refVal.schema;
73 $it.errSchemaPath = $schema;
74 var $code = it.validate($it).replace(/validate\.schema/g, $refVal.code);
75 out += ' ' + ($code) + ' ';
77 out += ' if (' + ($nextValid) + ') { ';
80 $async = $refVal.$async === true;
81 $refCode = $refVal.code;
85 var $$outStack = $$outStack || [];
88 if (it.opts.passContext) {
89 out += ' ' + ($refCode) + '.call(this, ';
91 out += ' ' + ($refCode) + '( ';
93 out += ' ' + ($data) + ', (dataPath || \'\')';
94 if (it.errorPath != '""') {
95 out += ' + ' + (it.errorPath);
97 var $parentData = $dataLvl ? 'data' + (($dataLvl - 1) || '') : 'parentData',
98 $parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
99 out += ' , ' + ($parentData) + ' , ' + ($parentDataProperty) + ', rootData) ';
100 var __callValidate = out;
101 out = $$outStack.pop();
103 if (!it.async) throw new Error('async schema referenced by sync schema');
106 out += 'var ' + ($valid) + ' =';
108 out += ' ' + (it.yieldAwait) + ' ' + (__callValidate) + '; } 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 += ' if (' + ($valid) + ') { ';
113 out += ' if (!' + (__callValidate) + ') { if (vErrors === null) vErrors = ' + ($refCode) + '.errors; else vErrors = vErrors.concat(' + ($refCode) + '.errors); errors = vErrors.length; } ';