garyvv /
node-sharp
| 1 | const ApiError = require('../util/api_error') |
||
| 2 | const Constant = require('../libraries/constant') |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 3 | |||
| 4 | module.exports = function (ctx, rules, message = {}) { |
||
| 5 | let msgKey |
||
| 6 | let input = ctx.input |
||
| 7 | |||
| 8 | for (let field in rules) { |
||
| 9 | rules[field].split('|').forEach(rule => { |
||
| 10 | // 普通的message key名称 |
||
| 11 | msgKey = field + '.' + rule |
||
| 12 | |||
| 13 | if (rule === 'required') { |
||
| 14 | if (!!input[field] === false) { |
||
| 15 | let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 不能为空' |
||
| 16 | throw new ApiError('validate.error', msg) |
||
| 17 | } |
||
| 18 | return true |
||
| 19 | } |
||
| 20 | |||
| 21 | // 非必填项,有值再进行判断 |
||
| 22 | if (!!input[field] === false) return true |
||
|
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. Loading history...
|
|||
| 23 | |||
| 24 | // 特殊条件 |
||
| 25 | if (rule.indexOf(':') > 0) { |
||
| 26 | let checkRule = rule.split(':') |
||
| 27 | msgKey = field + '.' + checkRule[0] |
||
| 28 | switch (checkRule[0]) { |
||
| 29 | case 'in': |
||
| 30 | let ruleData = checkRule[1].split(',') |
||
| 31 | // 数组 |
||
| 32 | if (ruleData instanceof Array === true) { |
||
| 33 | if (ruleData.indexOf(input[field]) === -1) { |
||
| 34 | let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 非法' |
||
| 35 | throw new ApiError('validate.error', msg) |
||
| 36 | } |
||
| 37 | } else { |
||
| 38 | let msg = '规则设置错误' |
||
| 39 | throw new ApiError('validate.error', msg) |
||
| 40 | } |
||
| 41 | break |
||
| 42 | case 'min': |
||
| 43 | // todo |
||
| 44 | break |
||
| 45 | case 'max': |
||
| 46 | // todo |
||
| 47 | break |
||
| 48 | default: |
||
| 49 | console.log('error “:” rule: ' + rule) |
||
|
0 ignored issues
–
show
|
|||
| 50 | let msg = '参数验证详细规则错误' |
||
| 51 | throw new ApiError('validate.error', msg) |
||
| 52 | break |
||
|
0 ignored issues
–
show
|
|||
| 53 | } |
||
| 54 | return true |
||
| 55 | } |
||
| 56 | |||
| 57 | // 简单条件 |
||
| 58 | switch (rule) { |
||
| 59 | case 'numeric': |
||
| 60 | if (isNaN(input[field]) === true) { |
||
| 61 | let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 必须为数字' |
||
| 62 | throw new ApiError('validate.error', msg) |
||
| 63 | } |
||
| 64 | break |
||
| 65 | default: |
||
| 66 | console.log('error rule: ' + rule) |
||
| 67 | let msg = '参数验证规则错误' |
||
| 68 | throw new ApiError('validate.error', msg) |
||
| 69 | break |
||
|
0 ignored issues
–
show
|
|||
| 70 | } |
||
| 71 | return true |
||
| 72 | }) |
||
| 73 | |||
| 74 | } |
||
| 75 | |||
| 76 | } |