Custom First reinvent what we had before the error handler
package AppLib
import (
"fmt"
"gopkg.in/go-playground/validator.v9"
"reflect"
"regexp"
)
//封装一个通用的正则方法,省去每次都要写下面这段很长的代码
func AddRegexTag(tagName string, pattern string, v *validator.Validate) error {
return v.RegisterValidation(tagName, func(fl validator.FieldLevel) bool {
m, _ := regexp.MatchString(pattern, fl.Field().String()) //返回bool和错误类型
return m
}, false) //为true则值为null也做判断,为false则不判断
}
func ValidErrMsg(obj interface{}, err error) error {
getObj := reflect.TypeOf(obj) //获取tag要用TypeOf
if err != nil {
if errs, ok := err.(validator.ValidationErrors); ok {
for _, e := range errs {
if f, exist := getObj.Elem().FieldByName(e.Field()); exist {
if value, ok := f.Tag.Lookup("vmsg"); ok { //查找tag中有没有我们自定义的错误消息,有就用自定义的没有就用默认的
return fmt.Errorf("%s", value)
} else {
return fmt.Errorf("%s", e.Value())
}
}
}
}
}
return nil
}
![](https://xiahualou.oss-cn-shanghai.aliyuncs.com/img/20200103145748.png)
As a rule, we use the built-in validator has failed to meet, we should use a regular here
func AddRegexTag(tagName string, pattern string, v *validator.Validate) error {
return v.RegisterValidation(tagName, func(fl validator.FieldLevel) bool {
m, _ := regexp.MatchString(pattern, fl.Field().String())
fmt.Println(fl.Field().String())
return m
}, true) //最后的参数true表示如果值是null也做判断,如果是false则不作判断
}
Analyzing field call validator
package main
import (
"fmt"
"gopkg.in/go-playground/validator.v9"
"log"
"micro/AppLib"
)
type Users struct {
Username string `validate:"required,min=6,max=20" vmsg:"用户名必须6位以上"`
Userpwd string `validate:"required,min=6,max=18" vmsg:"用户密码必须6位以上"`
Testname string `validate:"username" vmsg:"用户名规则不正确"` //这里的username对应v.RegisterValidation(tagName中的tagName,随便写写abc都可以但是要和它对应起来
}
func main() {
user := &Users{Username: "shenyi", Userpwd: "123123", Testname: "wqeqdasd"}
valid := validator.New()
//加入自定义的正则验证tag
err := AppLib.AddRegexTag("username", "[a-zA-Z]\\w{5,19}", valid)
if err != nil {
log.Fatal(err)
}
err = AppLib.ValidErrMsg(user, valid.Struct(user))
if err != nil {
log.Fatal(err)
}
fmt.Println("验证成功")
}