Skip to content

Commit 7144443

Browse files
author
奇淼(piexlmax
authored
Merge pull request flipped-aurora#492 from zooqkl/master
新增支持入参正则校验。
2 parents b5d4a4b + 43a213b commit 7144443

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

server/utils/validator.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utils
33
import (
44
"errors"
55
"reflect"
6+
"regexp"
67
"strconv"
78
"strings"
89
)
@@ -37,6 +38,15 @@ func NotEmpty() string {
3738
return "notEmpty"
3839
}
3940

41+
//@author: [zooqkl](https://github.com/zooqkl)
42+
//@function: RegexpMatch
43+
//@description: 正则校验 校验输入项是否满足正则表达式
44+
//@param: rule string
45+
//@return: string
46+
func RegexpMatch(rule string) string {
47+
return "regexp=" + rule
48+
}
49+
4050
//@author: [piexlmax](https://github.com/piexlmax)
4151
//@function: Lt
4252
//@description: 小于入参(<) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较
@@ -133,6 +143,10 @@ func Verify(st interface{}, roleMap Rules) (err error) {
133143
if isBlank(val) {
134144
return errors.New(tagVal.Name + "值不能为空")
135145
}
146+
case strings.Split(v, "=")[0] == "regexp":
147+
if !regexpMatch(strings.Split(v, "=")[1], val.String()) {
148+
return errors.New(tagVal.Name + "格式校验不通过")
149+
}
136150
case compareMap[strings.Split(v, "=")[0]]:
137151
if !compareVerify(val, v) {
138152
return errors.New(tagVal.Name + "长度或值不在合法范围," + v)
@@ -266,3 +280,7 @@ func compare(value interface{}, VerifyStr string) bool {
266280
return false
267281
}
268282
}
283+
284+
func regexpMatch(rule, matchStr string) bool {
285+
return regexp.MustCompile(rule).MatchString(matchStr)
286+
}

0 commit comments

Comments
 (0)