diff --git a/server/utils/validator.go b/server/utils/validator.go index 48ac6c674c49d630b5670d0392cea1a66c273861..5622f22d1a5041f2f17764a3dbdce48eaa0cb266 100644 --- a/server/utils/validator.go +++ b/server/utils/validator.go @@ -3,6 +3,7 @@ package utils import ( "errors" "reflect" + "regexp" "strconv" "strings" ) @@ -37,6 +38,15 @@ func NotEmpty() string { return "notEmpty" } +//@author: [zooqkl](https://github.com/zooqkl) +//@function: RegexpMatch +//@description: 正则校验 校验输入项是否满足正则表达式 +//@param: rule string +//@return: string +func RegexpMatch(rule string) string { + return "regexp=" + rule +} + //@author: [piexlmax](https://github.com/piexlmax) //@function: Lt //@description: 小于入参(<) 如果为string array Slice则为长度比较 如果是 int uint float 则为数值比较 @@ -133,6 +143,10 @@ func Verify(st interface{}, roleMap Rules) (err error) { if isBlank(val) { return errors.New(tagVal.Name + "值不能为空") } + case strings.Split(v, "=")[0] == "regexp": + if !regexpMatch(strings.Split(v, "=")[1], val.String()) { + return errors.New(tagVal.Name + "格式校验不通过") + } case compareMap[strings.Split(v, "=")[0]]: if !compareVerify(val, v) { return errors.New(tagVal.Name + "长度或值不在合法范围," + v) @@ -266,3 +280,7 @@ func compare(value interface{}, VerifyStr string) bool { return false } } + +func regexpMatch(rule, matchStr string) bool { + return regexp.MustCompile(rule).MatchString(matchStr) +}