2 module.exports = function generate_custom(it, $keyword, $ruleType) {
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;
11 var $data = 'data' + ($dataLvl || '');
12 var $valid = 'valid' + $lvl;
13 var $errs = 'errs__' + $lvl;
14 var $isData = it.opts.$data && $schema && $schema.$data,
17 out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
18 $schemaValue = 'schema' + $lvl;
20 $schemaValue = $schema;
23 $definition = 'definition' + $lvl,
24 $rDef = $rule.definition,
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;';
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;
40 var $ruleErrs = $validateCode + '.errors',
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;';
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) + ') { ';
58 if ($rDef.statements) {
59 out += ' ' + ($ruleValidate.validate) + ' ';
61 out += ' ' + ($valid) + ' = ' + ($ruleValidate.validate) + '; ';
64 var $it = it.util.copy(it);
65 var $closingBraces = '';
67 var $nextValid = 'valid' + $it.level;
68 $it.schema = $ruleValidate.validate;
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;
76 var $$outStack = $$outStack || [];
79 out += ' ' + ($validateCode) + '.call( ';
80 if (it.opts.passContext) {
85 if ($compile || $rDef.schema === false) {
86 out += ' , ' + ($data) + ' ';
88 out += ' , ' + ($schemaValue) + ' , ' + ($data) + ' , validate.schema' + (it.schemaPath) + ' ';
90 out += ' , (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 def_callRuleValidate = out;
98 out = $$outStack.pop();
99 if ($rDef.errors === false) {
100 out += ' ' + ($valid) + ' = ';
104 out += '' + (def_callRuleValidate) + '; ';
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; } ';
110 out += ' ' + ($ruleErrs) + ' = null; ' + ($valid) + ' = ' + (def_callRuleValidate) + '; ';
114 if ($rDef.modifying) {
115 out += ' if (' + ($parentData) + ') ' + ($data) + ' = ' + ($parentData) + '[' + ($parentDataProperty) + '];';
117 out += '' + ($closingBraces);
120 out += ' if (true) { ';
124 if ($rDef.valid === undefined) {
127 out += '' + ($nextValid);
129 out += '' + ($valid);
132 out += ' ' + (!$rDef.valid) + ' ';
135 $errorKeyword = $rule.keyword;
136 var $$outStack = $$outStack || [];
137 $$outStack.push(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\' ';
147 if (it.opts.verbose) {
148 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
155 out = $$outStack.pop();
156 if (!it.compositeRule && $breakOnError) {
157 /* istanbul ignore if */
159 out += ' throw new ValidationError([' + (__err) + ']); ';
161 out += ' validate.errors = [' + (__err) + ']; return false; ';
164 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
166 var def_customError = out;
167 out = $$outStack.pop();
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) + '; ';
178 if ($rDef.errors === false) {
179 out += ' ' + (def_customError) + ' ';
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) + '; ';
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\' ';
195 if (it.opts.verbose) {
196 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
202 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
203 if (!it.compositeRule && $breakOnError) {
204 /* istanbul ignore if */
206 out += ' throw new ValidationError(vErrors); ';
208 out += ' validate.errors = vErrors; return false; ';
212 if ($rDef.errors === false) {
213 out += ' ' + (def_customError) + ' ';
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) + '; ';
219 out += ' } } else { ' + (def_customError) + ' } ';