Schemagen is a tool to generate effective implementation of Validateable interface for your types to reduce validation overhead to ZERO.
Whether you choose to use code generation or runtime reflection traversal, validation will work either way.
Using reflection is easier because it does not require any codegen setup, but it does introduce minor performance decrease.
Unless performance is top-priority and validation is indeed a bottleneck (usually it's not), I'd recommend sticking with the reflection - it makes your codebase simpler to maintain. Though I've tried to make this tool as painless to use as go allows =)
WIP: no stable version yet
Go 1.24+ (with tool directive)
go get -tool github.com/metafates/schema/cmd/schemagen@main
This will add a tool directive to your go.mod
file
Then you can use it with go:generate
directive (notice the go tool
prefix)
//go:generate go tool schemagen -type Foo,Bar
type Foo struct {
A required.NonZero[string]
B optional.Negative[int]
}
type Bar map[string]MyStruct
Go 1.23 and earlier
See https://marcofranssen.nl/manage-go-tools-via-go-modules
Or:
go install github.com/metafates/schema/cmd/schemagen@main
Ensure that schemagen
is in your $PATH
:
which schemagen # should output something if everything is ok
Then you can use it with go:generate
directive
//go:generate schemagen -type Foo,Bar
type Foo struct {
A required.NonZero[string]
B optional.Negative[int]
}
type Bar map[string]MyStruct
And call go generate
as usual
go generate ./...
You should see the following files generated:
Foo.schema.go
Bar.schema.go
It generates YOUR_TYPE.schema.gen
file with Validate() error
method for each type specified.
Therefore validate.Validate(v any) error
will call this method instead of reflection-based field traversal.
That's it! It will reduce validataion overhead to almost zero.