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