Browse Source

Merge pull request #492 from zooqkl/master

新增支持入参正则校验。
main
奇淼(piexlmax 3 years ago
committed by GitHub
parent
commit
71444431f8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      server/utils/validator.go

18
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)
}
Loading…
Cancel
Save