Tag-based, no setup
Declare validation rules right next to your struct fields. No builders, no schema files — your struct is the schema.
Composable, i18n-ready, panic-safe — declared with struct tags, extended with your own checkers.
package main
import (
"fmt"
"github.com/wuhan005/govalid"
)
func main() {
v := struct {
Name string `valid:"required;username" label:"昵称"`
ID int `valid:"required;min:0;max:999" label:"用户编号"`
Mail string `valid:"required;email" label:"邮箱"`
}{
Name: "e99_",
ID: 1990,
Mail: "i@github.red.",
}
if errs, ok := govalid.Check(v); !ok {
for _, err := range errs {
fmt.Println(err)
}
}
}昵称的最后一个字符不能为下划线
用户编号应小于999
邮箱不是合法的电子邮箱格式| govalid | Bring-your-own if | |
|---|---|---|
| Where rules live | Beside the field, in struct tags | Scattered across handlers |
| Cross-field rules | equal: checker + Validate() | Manual conditionals |
| Localized messages | Built-in i18n + SetMessageTemplates | DIY |
| Custom rules | Checkers["myRule"] = … | Copy-paste functions |
| Safety on weird inputs | nil/map/embedded/unexported handled | Up to you |