go 验证库
validation
安装
go get github.com/dabao-zhao/validation@latest
使用
c := Customer{
Name: "dabao",
Email: "977904037@qq.com",
Address: Address{
Street: "5",
City: "Beijing",
State: "Beijing",
Zip: "0000000",
},
}
valid := validation.Make(&c,
validation.Field(&c.Address, rules.Required),
validation.Field(&c.Address.City, rules.Required, rules.Length(20, 100)),
validation.Field(&c.Address.Street, rules.Required, rules.RuneLength(20, 1000)),
)
err := valid.Validate()
c := map[string]interface{}{
"name": "dabao",
"email": "977904037@qq.com",
"address": map[string]interface{}{
"street": "5",
"city": "Beijing",
"state": "Beijing",
"zip": "0000000",
},
}
valid := validation.Make(&c,
validation.Field("address", rules.Required),
validation.Field("address.city", rules.Required, rules.Length(20, 100)),
)
err := valid.Validate()
灵感来源于
ozzo-validation,同时也使用了该库的很多方法
推荐文章: