golang初学者,在项目开发过程中造了一个简单的验证器轮子,欢迎大大们提宝贵建议和指导
go get github.com/smokezl/govalidators
import "github.com/smokezl/govalidators"
为 struct 指定验证器
package main
import "github.com/smokezl/govalidators"
type Class struct {
Cid int64 `validate:"required||integer=1,1000000"`
Cname string `validate:"required||string=1,5||unique"`
BeginTime string `validate:"required||datetime=H:i"`
}
type Student struct {
Uid int64 `validate:"required||integer=1,1000000"`
Name string `validate:"required||string=1,5"`
Age int64 `validate:"required||integer=10,30"`
Sex string `validate:"required||in=male,female"`
Email string `validate:"email"`
PersonalPage string `validate:"url"`
Hobby []string `validate:"array=_,2||unique||in=swimming,running,drawing"`
CreateTime string `validate:"datetime"`
Class []Class `validate:"array=1,3"`
}
验证
validator := govalidators.New()
if err := validator.Validate(student); err != nil {
fmt.Println(err)
}
type ValidatorF func(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)
自定义函数
func validationMethod(params map[string]interface{}, val reflect.Value, args ...string) (bool, error){
fmt.Println("validationMethod")
...
return true, nil
}
type Validator interface {
Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error)
}
自定义struct
type UserValidator struct {
EMsg string
}
func (self *UserValidator) Validate(params map[string]interface{}, val reflect.Value, args ...string) (bool, error) {
fmt.Println("UserValidator")
return true, nil
}
validator := govalidators.New()
validator.SetValidators(map[string]interface{}{
"user" : &UserValidator{},
"vm" : validationMethod,
})
Email string `validate:"email||user||vm"`
if err := validator.Validate(student); err != nil {
fmt.Println(err)
}
validator := govalidators.New()
validator.SetValidators(map[string]interface{}{
"string": &govalidators.StringValidator{
Range: govalidators.Range{
RangeEMsg: map[string]string{
"between": "[name] 长度必须在 [min] 和 [max] 之间",
},
},
},
"datetime": &govalidators.DateTimeValidator{
FmtStr: "Y-m-d",
},
"Email": &govalidators.EmailValidator{
Reg: `^(\d)+$`,
},
})
if err := validator.Validate(student); err != nil {
fmt.Println(err)
}
type Range struct {
Min string //最小值,外部可设置,支持0-9数字和 _ 符号,会将值赋值给 Range.min
Max string //最大值,外部可设置,支持0-9数字和 _ 符号,会将值赋值给 Range.max
min string //最小值,比对使用,支持0-9数字和 _ 符号,接收 Range.Min 和 struct 中传进来的值
max string //最大值,比对使用,支持0-9数字和 _ 符号,接收 Range.Max 和 struct 中传进来的值
/**
* 自定义范围判断错误 msg 格式,map 的 keys 有 lessThan,equal,atLeast,between ,根据类型的不同,msg 文案也不同,[min] 表示 Range.min, [max] 表示 Range.max
* var stringErrorMap = map[string]string{
* "lessThan": "[name] should be less than [max] chars long",
* "equal": "[name] should be equal [min] chars long",
* "atLeast": "[name] should be at least [min] chars long",
* "between": "[name] should be betwween [min] and [max] chars long",
* }
* var numberErrorMap = map[string]string{
* "lessThan": "[name] should be less than [max]",
* "equal": "[name] should be equal [min]",
* "atLeast": "[name] should be at least [min]",
* "between": "[name] should be betwween [min] and [max]",
* }
* var arrayErrorMap = map[string]string{
* "lessThan": "array [name] length should be less than [max]",
* "equal": "array [name] length should be equal [min]",
* "atLeast": "array [name] length should be at least [min]",
* "between": "array [name] length should be betwween [min] and [max]",
* }
*/
RangeEMsg map[string]string
}
type RequiredValidator struct{
EMsg string //自定义错误 msg 格式,默认为 [name] is must required,[name] 表示属性名,下同
}
type StringValidator struct{
EMsg string //自定义错误 msg 格式,默认为 [name] is not a string
Range //涉及到判断范围(字符串长度、数组长度、数字大小)验证器的公共属性
}
type IntegerValidator struct{
EMsg string //自定义错误 msg 格式,默认为 [name] is not a integer
Range //涉及到判断范围(字符串长度、数组长度、数字大小)验证器的公共属性
}
type ArrayValidator struct{
EMsg string //自定义错误 msg 格式,默认为 [name] is not a array/map/slice
Range //涉及到判断范围(字符串长度、数组长度、数字大小)验证器的公共属性
}
type EmailValidator struct{
EMsg string //自定义错误 msg 格式,默认为 [name] is not a email address
Reg string //自定义 email 正则
}
type UrlValidator struct{
EMsg string //自定义错误 msg 格式,默认为 [name] is not a url
Reg string //自定义 url 正则
}
8.in=?,?,?,?...,判断属性值是否在 in 后边定义的值中,仅支持 string、float、int、bool 类型或值类型为 string、float、int、bool 类型的array、slice、map
type InValidator struct{
EMsg string //自定义错误 msg 格式,默认为 [name] is not in params [args]
TypeEMsg string //自定义类型错误 msg 格式,默认为 [name] type invalid
}
type DateTimeValidator struct{
EMsg string //自定义错误 msg 格式,默认为 [name] is not a date time
FmtStr string //自定义Y m d H i s 组合,默认为 Y-m-d H:i:s
}
type UniqueValidator struct{
EMsg string //自定义错误 msg 格式,默认为 [name] is not unique
}
func (self *goValidator) SetTag(tag string) *goValidator
func (self *goValidator) SetSkipOnStructEmpty(skip bool) *goValidator
func (self *goValidator) SetValidatorSplit(str string) *goValidator
4.func (goValidator) SetValidator(validatorK string, validator interface{}),设置自定义验证器,验证器必须满足 ValidatorF 类型或实现 Validator 接口
func (self *goValidator) SetValidator(validatorK string, validator interface{}) *goValidator
5.func (goValidator) SetValidators(validatorMap map[string]interface{}),批量设置自定义验证器,验证器必须满足 ValidatorF 类型或实现 Validator 接口
func (self *goValidator) SetValidators(validatorMap map[string]interface{}) *goValidator
func (self *goValidator) LazyValidate(s interface{}) (err error)
func (self *goValidator) Validate(s interface{}) (err []error)
MIT licence.