2 module.exports = function generate_custom(it, $keyword) {
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.v5 && $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,
25 $validate = $rDef.validate,
26 $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 $schemaValue = 'validate.schema' + $schemaPath;
34 $validateCode = $ruleValidate.code;
35 $compile = $rDef.compile;
36 $inline = $rDef.inline;
39 var $ruleErrs = $validateCode + '.errors',
41 $ruleErr = 'ruleErr' + $lvl,
42 $asyncKeyword = $rDef.async;
43 if ($asyncKeyword && !it.async) throw new Error('async keyword in sync schema');
44 if (!($inline || $macro)) {
45 out += '' + ($ruleErrs) + ' = null;';
47 out += 'var ' + ($errs) + ' = errors;var ' + ($valid) + ';';
48 if ($validateSchema) {
49 out += ' ' + ($valid) + ' = ' + ($definition) + '.validateSchema(' + ($schemaValue) + '); if (' + ($valid) + ') {';
52 if ($rDef.statements) {
53 out += ' ' + ($ruleValidate.validate) + ' ';
55 out += ' ' + ($valid) + ' = ' + ($ruleValidate.validate) + '; ';
58 var $it = it.util.copy(it);
60 var $nextValid = 'valid' + $it.level;
61 $it.schema = $ruleValidate.validate;
63 var $wasComposite = it.compositeRule;
64 it.compositeRule = $it.compositeRule = true;
65 var $code = it.validate($it).replace(/validate\.schema/g, $validateCode);
66 it.compositeRule = $it.compositeRule = $wasComposite;
69 var $$outStack = $$outStack || [];
72 out += ' ' + ($validateCode) + '.call( ';
73 if (it.opts.passContext) {
78 if ($compile || $rDef.schema === false) {
79 out += ' , ' + ($data) + ' ';
81 out += ' , ' + ($schemaValue) + ' , ' + ($data) + ' , validate.schema' + (it.schemaPath) + ' ';
83 out += ' , (dataPath || \'\')';
84 if (it.errorPath != '""') {
85 out += ' + ' + (it.errorPath);
87 var $parentData = $dataLvl ? 'data' + (($dataLvl - 1) || '') : 'parentData',
88 $parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
89 out += ' , ' + ($parentData) + ' , ' + ($parentDataProperty) + ' , rootData ) ';
90 var def_callRuleValidate = out;
91 out = $$outStack.pop();
92 if ($rDef.errors === false) {
93 out += ' ' + ($valid) + ' = ';
95 out += '' + (it.yieldAwait);
97 out += '' + (def_callRuleValidate) + '; ';
100 $ruleErrs = 'customErrors' + $lvl;
101 out += ' var ' + ($ruleErrs) + ' = null; try { ' + ($valid) + ' = ' + (it.yieldAwait) + (def_callRuleValidate) + '; } catch (e) { ' + ($valid) + ' = false; if (e instanceof ValidationError) ' + ($ruleErrs) + ' = e.errors; else throw e; } ';
103 out += ' ' + ($ruleErrs) + ' = null; ' + ($valid) + ' = ' + (def_callRuleValidate) + '; ';
107 if ($rDef.modifying) {
108 out += ' ' + ($data) + ' = ' + ($parentData) + '[' + ($parentDataProperty) + '];';
110 if ($validateSchema) {
115 out += ' if (true) { ';
119 if ($rDef.valid === undefined) {
122 out += '' + ($nextValid);
124 out += '' + ($valid);
127 out += ' ' + (!$rDef.valid) + ' ';
130 $errorKeyword = $rule.keyword;
131 var $$outStack = $$outStack || [];
132 $$outStack.push(out);
134 var $$outStack = $$outStack || [];
135 $$outStack.push(out);
136 out = ''; /* istanbul ignore else */
137 if (it.createErrors !== false) {
138 out += ' { keyword: \'' + ($errorKeyword || 'custom') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { keyword: \'' + ($rule.keyword) + '\' } ';
139 if (it.opts.messages !== false) {
140 out += ' , message: \'should pass "' + ($rule.keyword) + '" keyword validation\' ';
142 if (it.opts.verbose) {
143 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
150 out = $$outStack.pop();
151 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
153 out += ' throw new ValidationError([' + (__err) + ']); ';
155 out += ' validate.errors = [' + (__err) + ']; return false; ';
158 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
160 var def_customError = out;
161 out = $$outStack.pop();
164 if ($rDef.errors != 'full') {
165 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) + '"; } ';
166 if (it.opts.verbose) {
167 out += ' ' + ($ruleErr) + '.schema = ' + ($schemaValue) + '; ' + ($ruleErr) + '.data = ' + ($data) + '; ';
172 if ($rDef.errors === false) {
173 out += ' ' + (def_customError) + ' ';
175 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) + '"; } ';
176 if (it.opts.verbose) {
177 out += ' ' + ($ruleErr) + '.schema = ' + ($schemaValue) + '; ' + ($ruleErr) + '.data = ' + ($data) + '; ';
183 out += ' var err = '; /* istanbul ignore else */
184 if (it.createErrors !== false) {
185 out += ' { keyword: \'' + ($errorKeyword || 'custom') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { keyword: \'' + ($rule.keyword) + '\' } ';
186 if (it.opts.messages !== false) {
187 out += ' , message: \'should pass "' + ($rule.keyword) + '" keyword validation\' ';
189 if (it.opts.verbose) {
190 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
196 out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
197 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
199 out += ' throw new ValidationError(vErrors); ';
201 out += ' validate.errors = vErrors; return false; ';
205 if ($rDef.errors === false) {
206 out += ' ' + (def_customError) + ' ';
208 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) + '"; ';
209 if (it.opts.verbose) {
210 out += ' ' + ($ruleErr) + '.schema = ' + ($schemaValue) + '; ' + ($ruleErr) + '.data = ' + ($data) + '; ';
212 out += ' } } else { ' + (def_customError) + ' } ';