| 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 += '' + (it.yieldAwait); |
| 103 | } |
| 104 | out += '' + (def_callRuleValidate) + '; '; |
| 105 | } else { |
| 106 | if ($asyncKeyword) { |
| 107 | $ruleErrs = 'customErrors' + $lvl; |
| 108 | out += ' var ' + ($ruleErrs) + ' = null; try { ' + ($valid) + ' = ' + (it.yieldAwait) + (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) { /* istanbul ignore if */ |
| 157 | if (it.async) { |
| 158 | out += ' throw new ValidationError([' + (__err) + ']); '; |
| 159 | } else { |
| 160 | out += ' validate.errors = [' + (__err) + ']; return false; '; |
| 161 | } |
| 162 | } else { |
| 163 | out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; |
| 164 | } |
| 165 | var def_customError = out; |
| 166 | out = $$outStack.pop(); |
| 167 | if ($inline) { |
| 168 | if ($rDef.errors) { |
| 169 | if ($rDef.errors != 'full') { |
| 170 | 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) + '"; } '; |
| 171 | if (it.opts.verbose) { |
| 172 | out += ' ' + ($ruleErr) + '.schema = ' + ($schemaValue) + '; ' + ($ruleErr) + '.data = ' + ($data) + '; '; |
| 173 | } |
| 174 | out += ' } '; |
| 175 | } |
| 176 | } else { |
| 177 | if ($rDef.errors === false) { |
| 178 | out += ' ' + (def_customError) + ' '; |
| 179 | } else { |
| 180 | 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) + '"; } '; |
| 181 | if (it.opts.verbose) { |
| 182 | out += ' ' + ($ruleErr) + '.schema = ' + ($schemaValue) + '; ' + ($ruleErr) + '.data = ' + ($data) + '; '; |
| 183 | } |
| 184 | out += ' } } '; |
| 185 | } |
| 186 | } |
| 187 | } else if ($macro) { |
| 188 | out += ' var err = '; /* istanbul ignore else */ |
| 189 | if (it.createErrors !== false) { |
| 190 | out += ' { keyword: \'' + ($errorKeyword || 'custom') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { keyword: \'' + ($rule.keyword) + '\' } '; |
| 191 | if (it.opts.messages !== false) { |
| 192 | out += ' , message: \'should pass "' + ($rule.keyword) + '" keyword validation\' '; |
| 193 | } |
| 194 | if (it.opts.verbose) { |
| 195 | out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' '; |
| 196 | } |
| 197 | out += ' } '; |
| 198 | } else { |
| 199 | out += ' {} '; |
| 200 | } |
| 201 | out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; |
| 202 | if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */ |
| 203 | if (it.async) { |
| 204 | out += ' throw new ValidationError(vErrors); '; |
| 205 | } else { |
| 206 | out += ' validate.errors = vErrors; return false; '; |
| 207 | } |
| 208 | } |
| 209 | } else { |
| 210 | if ($rDef.errors === false) { |
| 211 | out += ' ' + (def_customError) + ' '; |
| 212 | } else { |
| 213 | 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) + '"; '; |
| 214 | if (it.opts.verbose) { |
| 215 | out += ' ' + ($ruleErr) + '.schema = ' + ($schemaValue) + '; ' + ($ruleErr) + '.data = ' + ($data) + '; '; |
| 216 | } |
| 217 | out += ' } } else { ' + (def_customError) + ' } '; |
| 218 | } |
| 219 | } |
| 220 | out += ' } '; |
| 221 | if ($breakOnError) { |
| 222 | out += ' else { '; |
| 223 | } |
| 224 | } |
| 225 | return out; |
| 226 | } |