topbit 3.1.4 → 3.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.cn.md +3 -3
- package/README.md +3 -3
- package/demo/rule.js +115 -0
- package/package.json +1 -1
- package/src/extends/paramcheck.js +14 -14
package/README.cn.md
CHANGED
|
@@ -2008,7 +2008,7 @@ const {ParamCheck} = Topbit.extensions
|
|
|
2008
2008
|
// Query 参数检测
|
|
2009
2009
|
let pck = new ParamCheck({
|
|
2010
2010
|
key: 'query',
|
|
2011
|
-
|
|
2011
|
+
rule: {
|
|
2012
2012
|
// 严格限制值
|
|
2013
2013
|
say: 'hello',
|
|
2014
2014
|
// 类型转换与范围限制
|
|
@@ -2026,7 +2026,7 @@ let paramck = new ParamCheck({
|
|
|
2026
2026
|
key: 'param',
|
|
2027
2027
|
deny: ['x-key'], // 禁止提交的字段
|
|
2028
2028
|
deleteDeny: true,
|
|
2029
|
-
|
|
2029
|
+
rule: {
|
|
2030
2030
|
errorMessage: '参数错误', // 自定义错误信息
|
|
2031
2031
|
mobile: {
|
|
2032
2032
|
callback: (obj, k, method) => {
|
|
@@ -2046,7 +2046,7 @@ app.use(pck, {method: 'GET'})
|
|
|
2046
2046
|
```javascript
|
|
2047
2047
|
let pmbody = new ParamCheck({
|
|
2048
2048
|
key: 'body',
|
|
2049
|
-
|
|
2049
|
+
rule: {
|
|
2050
2050
|
username: { must: true },
|
|
2051
2051
|
passwd: { must: true }
|
|
2052
2052
|
}
|
package/README.md
CHANGED
|
@@ -1960,7 +1960,7 @@ const {ParamCheck} = Topbit.extensions
|
|
|
1960
1960
|
// Query parameter check
|
|
1961
1961
|
let pck = new ParamCheck({
|
|
1962
1962
|
key: 'query',
|
|
1963
|
-
|
|
1963
|
+
rule: {
|
|
1964
1964
|
// Strictly restrict value
|
|
1965
1965
|
say: 'hello',
|
|
1966
1966
|
// Type conversion and range restriction
|
|
@@ -1978,7 +1978,7 @@ let paramck = new ParamCheck({
|
|
|
1978
1978
|
key: 'param',
|
|
1979
1979
|
deny: ['x-key'], // Fields forbidden to submit
|
|
1980
1980
|
deleteDeny: true,
|
|
1981
|
-
|
|
1981
|
+
rule: {
|
|
1982
1982
|
errorMessage: 'Parameter Error', // Custom error message
|
|
1983
1983
|
mobile: {
|
|
1984
1984
|
callback: (obj, k, method) => {
|
|
@@ -1998,7 +1998,7 @@ app.use(pck, {method: 'GET'})
|
|
|
1998
1998
|
```javascript
|
|
1999
1999
|
let pmbody = new ParamCheck({
|
|
2000
2000
|
key: 'body',
|
|
2001
|
-
|
|
2001
|
+
rule: {
|
|
2002
2002
|
username: { must: true },
|
|
2003
2003
|
passwd: { must: true }
|
|
2004
2004
|
}
|
package/demo/rule.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
let Topbit = require('../src/topbit.js')
|
|
4
|
+
|
|
5
|
+
let {ParamCheck} = Topbit.extensions
|
|
6
|
+
|
|
7
|
+
let app = new Topbit({
|
|
8
|
+
debug: true
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
let pck = new ParamCheck({
|
|
12
|
+
//支持query、param、body,对应于请求上下文的ctx.query、ctx.param、ctx.body。
|
|
13
|
+
key: 'query',
|
|
14
|
+
|
|
15
|
+
//要验证的数据,key值即为属性名称,验证规则可以是string|number|object。
|
|
16
|
+
//string会严格判等,number仅仅数据判等,object是最强大的功能。
|
|
17
|
+
rule: {
|
|
18
|
+
//严格限制say的值必须是hello。
|
|
19
|
+
say: 'hello',
|
|
20
|
+
offset: {
|
|
21
|
+
//如果c.query.offset是undefined,则会赋值为0。
|
|
22
|
+
default: 0,
|
|
23
|
+
//要转换的类型,只能是int、float、boolean
|
|
24
|
+
to: 'int',
|
|
25
|
+
//最小值,>=
|
|
26
|
+
min: 0,
|
|
27
|
+
//最大值,<=
|
|
28
|
+
max: 100
|
|
29
|
+
},
|
|
30
|
+
test: {
|
|
31
|
+
default: false,
|
|
32
|
+
// 转换为布尔类型,若字符串为true则转换为布尔值true,否则转换为布尔值false。
|
|
33
|
+
to: 'boolean'
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
let paramck = new ParamCheck({
|
|
40
|
+
//支持query或param,对应于请求上下文的ctx.query和ctx.param。
|
|
41
|
+
key: 'param',
|
|
42
|
+
|
|
43
|
+
//禁止提交的字段
|
|
44
|
+
deny: ['x-key', 'test'],
|
|
45
|
+
|
|
46
|
+
//检测到存在禁止提交的属性则自动删除,默认会返回400错误。
|
|
47
|
+
deleteDeny: true,
|
|
48
|
+
|
|
49
|
+
//要验证的数据,key值即为属性名称,验证规则可以是string|number|object。
|
|
50
|
+
//string会严格判等,number仅仅数据判等,object是最强大的功能。
|
|
51
|
+
rule: {
|
|
52
|
+
//自定义错误返回的消息,每个属性都可以有自己的错误消息提示。
|
|
53
|
+
name: {
|
|
54
|
+
errorMessage: 'name长度必须在2~8范围内。',
|
|
55
|
+
|
|
56
|
+
//obj是c.query或c.param,k是属性名称,method是当前请求方法
|
|
57
|
+
callback: (obj, k, method) => {
|
|
58
|
+
if (obj[k].length < 2 || obj[k].length > 8) {
|
|
59
|
+
return false
|
|
60
|
+
}
|
|
61
|
+
return true
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
age: {
|
|
66
|
+
errorMessage: '年龄必须在12~65',
|
|
67
|
+
to: 'int',
|
|
68
|
+
min: 12,
|
|
69
|
+
max: 65
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
mobile: {
|
|
73
|
+
errorMessage: '手机号不符合要求',
|
|
74
|
+
//利用callback,可以实现完全自主的自定义规则。
|
|
75
|
+
callback: (obj, k, method) => {
|
|
76
|
+
let preg = /^(12|13|15|16|17|18|19)[0-9]{9}$/
|
|
77
|
+
if (!preg.test(obj[k])) {
|
|
78
|
+
return false
|
|
79
|
+
}
|
|
80
|
+
return true
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
let pmbody = new ParamCheck({
|
|
88
|
+
key: 'body',
|
|
89
|
+
rule: {
|
|
90
|
+
username: {
|
|
91
|
+
//必须有这个属性。
|
|
92
|
+
must: true
|
|
93
|
+
},
|
|
94
|
+
passwd: {
|
|
95
|
+
must: true
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
app.use(pck, {method: 'GET'})
|
|
101
|
+
.use(paramck, {method: 'GET'})
|
|
102
|
+
.use(pmbody, {method: ['POST', 'PUT'], name: 'login'})
|
|
103
|
+
|
|
104
|
+
app.get('/user/:name/:age/:mobile', async c => {
|
|
105
|
+
c.ok({
|
|
106
|
+
query: c.query,
|
|
107
|
+
param: c.param
|
|
108
|
+
})
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
app.post('/login', async c => {
|
|
112
|
+
c.ok(c.body)
|
|
113
|
+
}, {name: 'login'})
|
|
114
|
+
|
|
115
|
+
app.run(1234)
|
package/package.json
CHANGED
|
@@ -13,7 +13,7 @@ class ParamCheck {
|
|
|
13
13
|
constructor(options = {}) {
|
|
14
14
|
this.type = ['query', 'param', 'body']
|
|
15
15
|
this.key = 'param'
|
|
16
|
-
this.
|
|
16
|
+
this.rule = {}
|
|
17
17
|
this.errorMessage = "提交数据不符合要求"
|
|
18
18
|
//设置禁止提交的字段
|
|
19
19
|
this.deny = null
|
|
@@ -28,9 +28,9 @@ class ParamCheck {
|
|
|
28
28
|
}
|
|
29
29
|
break
|
|
30
30
|
|
|
31
|
-
case '
|
|
31
|
+
case 'rule':
|
|
32
32
|
if (typeof options[k] === 'object') {
|
|
33
|
-
this.
|
|
33
|
+
this.rule = options[k]
|
|
34
34
|
}
|
|
35
35
|
break
|
|
36
36
|
|
|
@@ -57,25 +57,25 @@ class ParamCheck {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
let data_type = ''
|
|
60
|
-
for (let k in this.
|
|
61
|
-
data_type = typeof this.
|
|
60
|
+
for (let k in this.rule) {
|
|
61
|
+
data_type = typeof this.rule[k]
|
|
62
62
|
|
|
63
63
|
if (data_type === 'string' || data_type === 'number') {
|
|
64
|
-
this.
|
|
64
|
+
this.rule[k] = {
|
|
65
65
|
__is_value__: true,
|
|
66
|
-
__value__: this.
|
|
66
|
+
__value__: this.rule[k],
|
|
67
67
|
__type__: data_type === 'string' ? TYPE_STRING : TYPE_NUMBER
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
continue
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
this.
|
|
73
|
+
this.rule[k].__is_value__ = false
|
|
74
74
|
|
|
75
|
-
if (this.
|
|
76
|
-
this.
|
|
75
|
+
if (this.rule[k].callback && typeof this.rule[k].callback === 'function') {
|
|
76
|
+
this.rule[k].__is_call__ = true
|
|
77
77
|
} else {
|
|
78
|
-
this.
|
|
78
|
+
this.rule[k].__is_call__ = false
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
|
|
@@ -194,8 +194,8 @@ class ParamCheck {
|
|
|
194
194
|
let ost = {ok: true, key: ''}
|
|
195
195
|
|
|
196
196
|
if (this.key !== 'body' || (c.body !== c.rawBody && typeof c.body === 'object')) {
|
|
197
|
-
for (let k in this.
|
|
198
|
-
if (!this.checkData(d, k, this.
|
|
197
|
+
for (let k in this.rule) {
|
|
198
|
+
if (!this.checkData(d, k, this.rule[k], c.method, ost)) {
|
|
199
199
|
return ost
|
|
200
200
|
}
|
|
201
201
|
}
|
|
@@ -206,7 +206,7 @@ class ParamCheck {
|
|
|
206
206
|
|
|
207
207
|
mid() {
|
|
208
208
|
let self = this
|
|
209
|
-
let dataObject = this.
|
|
209
|
+
let dataObject = this.rule
|
|
210
210
|
|
|
211
211
|
if (!Array.isArray(this.deny) || this.deny.length === 0) this.deny = null
|
|
212
212
|
|