未验证 提交 71444431 编写于 作者: Mr.奇淼('s avatar Mr.奇淼( 提交者: GitHub

Merge pull request #492 from zooqkl/master

新增支持入参正则校验。
......@@ -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)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册