govalid generates type-safe validation code from struct field markers. No reflection, no runtime overhead β just fast validation.
- Zero allocations β All validation performs no heap allocations
- 5-44x faster β Outperforms reflection-based validators
- Type safe β Errors caught at generation time, not runtime
- Full collection support β Maps, channels, slices, and arrays (not just slices)
- CEL expressions β Complex validation with Common Expression Language
go install github.com/sivchari/govalid/cmd/govalid@latestVerify the installation:
govalid -h//govalid:required
type Person struct {
Name string `json:"name"`
//govalid:email
Email string `json:"email"`
}govalid ./...func main() {
p := &Person{Name: "John", Email: "invalid-email"}
if err := p.Validate(); err != nil {
log.Printf("Validation failed: %v", err)
}
}For complete details, see MARKERS.md.
| Marker | Description | Example |
|---|---|---|
required |
Field must not be zero value | //govalid:required |
gt |
Greater than | //govalid:gt=0 |
gte |
Greater than or equal | //govalid:gte=1 |
lt |
Less than | //govalid:lt=100 |
lte |
Less than or equal | //govalid:lte=99 |
maxlength |
Maximum string length | //govalid:maxlength=255 |
minlength |
Minimum string length | //govalid:minlength=1 |
maxitems |
Maximum collection size | //govalid:maxitems=10 |
minitems |
Minimum collection size | //govalid:minitems=1 |
enum |
Must be one of specified values | //govalid:enum=active,inactive |
| Marker | Description |
|---|---|
email |
Valid email address |
url |
Valid URL |
uuid |
Valid UUID |
numeric |
Numeric string |
| Marker | Description | Example |
|---|---|---|
cel |
CEL expression | //govalid:cel=value >= 18 |
Apply markers to the entire struct:
//govalid:required
type Person struct {
Name string // required
Email string // required
Age int // required
}Use Common Expression Language for complex validation:
type User struct {
//govalid:cel=value >= 18 && value <= 120
Age int
//govalid:cel=value >= this.Age
RetirementAge int
}Validate maps, channels, slices, and arrays:
type UserList struct {
//govalid:maxitems=10
Users []User
//govalid:minitems=1
UserMap map[string]User
//govalid:maxitems=5
UserChan chan User
}if err := p.Validate(); err != nil {
log.Printf("Validation failed: %v", err)
}if err := p.Validate(); err != nil {
if errors.Is(err, ErrPersonEmailEmailValidation) {
// Handle email error
}
if errors.Is(err, ErrPersonNameRequiredValidation) {
// Handle required error
}
}import "github.com/sivchari/govalid/validation/middleware"
func main() {
http.HandleFunc("/person", middleware.ValidateRequest[*Person](handler))
http.ListenAndServe(":8080", nil)
}govalid outperforms reflection-based validators by 5x to 44x with zero allocations.
| Validator | govalid | go-playground | Improvement |
|---|---|---|---|
| Required | 1.9ns | 85.5ns | 44x |
| GT/LT | 1.9ns | 63.0ns | 32x |
| MaxLength | 15.7ns | 73.5ns | 5x |
| 38.2ns | 649.4ns | 17x |
See benchmark details for more information.
git clone https://github.com/sivchari/govalid.git
cd govalid
make install-lefthookgo install ./cmd/govalid/MIT License - see LICENSE for details.