-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
107 lines (99 loc) · 4.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
module.exports = function (input, rules, message = {}) {
let msgKey
for (let field in rules) {
rules[field].split('|').forEach(rule => {
// 普通的message key名称
msgKey = field + '.' + rule
if (rule === 'required') {
if (!!input[field] === false) {
let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 不能为空'
throw new Error(msg)
}
return true
}
// 非必填项,有值再进行判断
if (!!input[field] === false) return true
// 特殊条件
if (rule.indexOf(':') > 0) {
let checkRule = rule.split(':')
msgKey = field + '.' + checkRule[0]
switch (checkRule[0]) {
case 'in':
let ruleData = checkRule[1].split(',')
// 数组
if (ruleData instanceof Array === true) {
field = field.toString()
input[field] = input[field].toString()
if (ruleData.indexOf(input[field]) === -1) {
let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 非法'
throw new Error(msg)
}
} else {
let msg = '规则设置错误'
throw new Error(msg)
}
break
case 'min':
let min = parseInt(checkRule[1])
if (isNaN(input[field]) === true) {
// 字符串
if (input[field].length < min) {
let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 不能小于' + min + '个字符'
throw new Error(msg)
}
} else {
// 数字
if (input[field] < min) {
let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 不能小于' + min
throw new Error(msg)
}
}
break
case 'max':
let max = parseInt(checkRule[1])
if (isNaN(input[field]) === true) {
// 字符串
if (input[field].length > max) {
let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 不能大于' + max + '个字符'
throw new Error(msg)
}
} else {
// 数字
if (input[field] > max) {
let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 不能大于' + max
throw new Error(msg)
}
}
break
default:
console.log('error “:” rule: ' + rule)
let msg = '参数验证详细规则错误'
throw new Error(msg)
break
}
return true
}
// 简单条件
switch (rule) {
case 'numeric':
if (isNaN(input[field]) === true) {
let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 必须为数字'
throw new Error(msg)
}
break
case 'array':
if (Object.prototype.toString.call(input[field]) !== '[object Array]') {
let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 必须为数组'
throw new Error(msg)
}
break
default:
console.log('error rule: ' + rule)
let msg = '参数验证规则错误'
throw new Error(msg)
break
}
return true
})
}
}