简要说明
// r 为 *http.Request
// 结构体增加标签 validate(验证规则) 和 fieldName(显示的字段别名)如下u为结构体的实例
bool,string = validate.StructValidate(r, u)
// map数据验证 map[字段名][]string{"规则1","规则2"...} , 别名(选传)map[字段名]别名
bool,string = validate.MapValidate(r, mapData,fieldMap)
完整示例
import (
"github.com/wuyan94zl/gowebpkg/validate"
)
type User struct {
Id uint32 `validate:"numeric"fieldName:"ID"`
Name string `validate:"required||min:6||max:32"fieldName:"用户名称"`
}
u := User{Id: 2, Name: "无言"}
r := &http.Request{}
if ok, msg := validate.StructValidate(r, u); !ok {
fmt.Println(msg)
}
mapData := make(map[string][]string)
mapData["Id"] = []string{"numeric"}
mapData["Name"] = []string{"required","min:10","max:32"}
mapMsg := make(map[string]string)
mapMsg["Id"] = "ID"
mapMsg["Name"] = "会员名称"
// mapMsg 可以不传如: validate.MapValidate(r, mapData)
if ok, msg := validate.MapValidate(r, mapData,mapMsg); !ok {
fmt.Println(msg)
}