A Go library for validating structs, maps and slices.
Simple
Simple and stupid, no magic involved.
Type-safe
Schema is defined in Go, which is type-safer (and more powerful) than traditional struct tags.
Flexible
No reflection
$ go get github.com/RussellLuo/validating/v3@latest
package main
import (
"fmt"
v "github.com/RussellLuo/validating/v3"
)
type Address struct {
Country string
City string
}
func (a Address) Schema() v.Schema {
return v.Schema{
v.F("country", a.Country): v.Nonzero[string]().Msg("empty country"),
v.F("city", a.City): v.In("A", "B", "C").Msg("must be A or B or C"),
}
}
type Person struct {
Name string
Age int
Address Address
}
func (p Person) Schema() v.Schema {
return v.Schema{
v.F("name", p.Name): v.LenString(1, 5).Msg("bad name"),
v.F("age", p.Age): v.Gte(10).Msg("must be older than 10 years old"),
v.F("address", p.Address): p.Address.Schema(),
}
}
func main() {
p := Person{}
errs := v.Validate(p.Schema())
for _, err := range errs {
fmt.Println(err)
}
}
$ go run main.go
name: INVALID(bad name)
age: INVALID(must be older than 10 years old)
address.country: INVALID(empty country)
address.city: INVALID(must be A or B or C)
To be strict, this library has a conceptual distinction between validator factory and validator.
A validator factory is a function used to create a validator, which will do the actual validation.
Check out the Godoc.
This library borrows some ideas from the following libraries:
Prefer no reflection.
Support composite validator factories All/And, Any/Or.
Use the term nonzero instead of required/optional.
$ claude mcp add validating \
-- python -m otcore.mcp_server <graph>