1 | 'use strict';
|
---|
2 | module.exports = function generate_ref(it, $keyword, $ruleType) {
|
---|
3 | var out = ' ';
|
---|
4 | var $lvl = it.level;
|
---|
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;
|
---|
11 | var $async, $refCode;
|
---|
12 | if ($schema == '#' || $schema == '#/') {
|
---|
13 | if (it.isRoot) {
|
---|
14 | $async = it.async;
|
---|
15 | $refCode = 'validate';
|
---|
16 | } else {
|
---|
17 | $async = it.root.schema.$async === true;
|
---|
18 | $refCode = 'root.refVal[0]';
|
---|
19 | }
|
---|
20 | } else {
|
---|
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 || [];
|
---|
27 | $$outStack.push(out);
|
---|
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)) + '\' ';
|
---|
33 | }
|
---|
34 | if (it.opts.verbose) {
|
---|
35 | out += ' , schema: ' + (it.util.toQuotedString($schema)) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
|
---|
36 | }
|
---|
37 | out += ' } ';
|
---|
38 | } else {
|
---|
39 | out += ' {} ';
|
---|
40 | }
|
---|
41 | var __err = out;
|
---|
42 | out = $$outStack.pop();
|
---|
43 | if (!it.compositeRule && $breakOnError) {
|
---|
44 | /* istanbul ignore if */
|
---|
45 | if (it.async) {
|
---|
46 | out += ' throw new ValidationError([' + (__err) + ']); ';
|
---|
47 | } else {
|
---|
48 | out += ' validate.errors = [' + (__err) + ']; return false; ';
|
---|
49 | }
|
---|
50 | } else {
|
---|
51 | out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
|
---|
52 | }
|
---|
53 | if ($breakOnError) {
|
---|
54 | out += ' if (false) { ';
|
---|
55 | }
|
---|
56 | } else if (it.opts.missingRefs == 'ignore') {
|
---|
57 | it.logger.warn($message);
|
---|
58 | if ($breakOnError) {
|
---|
59 | out += ' if (true) { ';
|
---|
60 | }
|
---|
61 | } else {
|
---|
62 | throw new it.MissingRefError(it.baseId, $schema, $message);
|
---|
63 | }
|
---|
64 | } else if ($refVal.inline) {
|
---|
65 | var $it = it.util.copy(it);
|
---|
66 | $it.level++;
|
---|
67 | var $nextValid = 'valid' + $it.level;
|
---|
68 | $it.schema = $refVal.schema;
|
---|
69 | $it.schemaPath = '';
|
---|
70 | $it.errSchemaPath = $schema;
|
---|
71 | var $code = it.validate($it).replace(/validate\.schema/g, $refVal.code);
|
---|
72 | out += ' ' + ($code) + ' ';
|
---|
73 | if ($breakOnError) {
|
---|
74 | out += ' if (' + ($nextValid) + ') { ';
|
---|
75 | }
|
---|
76 | } else {
|
---|
77 | $async = $refVal.$async === true || (it.async && $refVal.$async !== false);
|
---|
78 | $refCode = $refVal.code;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | if ($refCode) {
|
---|
82 | var $$outStack = $$outStack || [];
|
---|
83 | $$outStack.push(out);
|
---|
84 | out = '';
|
---|
85 | if (it.opts.passContext) {
|
---|
86 | out += ' ' + ($refCode) + '.call(this, ';
|
---|
87 | } else {
|
---|
88 | out += ' ' + ($refCode) + '( ';
|
---|
89 | }
|
---|
90 | out += ' ' + ($data) + ', (dataPath || \'\')';
|
---|
91 | if (it.errorPath != '""') {
|
---|
92 | out += ' + ' + (it.errorPath);
|
---|
93 | }
|
---|
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();
|
---|
99 | if ($async) {
|
---|
100 | if (!it.async) throw new Error('async schema referenced by sync schema');
|
---|
101 | if ($breakOnError) {
|
---|
102 | out += ' var ' + ($valid) + '; ';
|
---|
103 | }
|
---|
104 | out += ' try { await ' + (__callValidate) + '; ';
|
---|
105 | if ($breakOnError) {
|
---|
106 | out += ' ' + ($valid) + ' = true; ';
|
---|
107 | }
|
---|
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; ';
|
---|
109 | if ($breakOnError) {
|
---|
110 | out += ' ' + ($valid) + ' = false; ';
|
---|
111 | }
|
---|
112 | out += ' } ';
|
---|
113 | if ($breakOnError) {
|
---|
114 | out += ' if (' + ($valid) + ') { ';
|
---|
115 | }
|
---|
116 | } else {
|
---|
117 | out += ' if (!' + (__callValidate) + ') { if (vErrors === null) vErrors = ' + ($refCode) + '.errors; else vErrors = vErrors.concat(' + ($refCode) + '.errors); errors = vErrors.length; } ';
|
---|
118 | if ($breakOnError) {
|
---|
119 | out += ' else { ';
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 | return out;
|
---|
124 | }
|
---|