govalid
High-performance, type-safe validation library for Go with zero allocations
Overview
govalid is a Go package designed to generate type-safe validation code for structs based on markers. It provides a mechanism to apply validation rules directly in the code by marking struct fields with specific markers. The tool processes these markers and generates corresponding validation functions with zero heap allocations.
Key Features
- 🚀 Zero Allocations: All validators perform zero heap allocations
- 🔒 Type Safety: Generated validation functions with proper types
- ⚡ High Performance: 5x to 44x faster than reflection-based validators
- 📝 Marker-Based: Simple comment-based validation rules
- 🔧 Code Generation: Generates optimized validation functions
- 🎯 Comprehensive: Support for all common validation patterns
Quick Start
Installation
|
|
Basic Usage
- Define your struct with markers:
|
|
- Generate validation code:
|
|
This generates validation code like:
|
|
Note: Error variable names include the struct name as a prefix (e.g., ErrUserNameRequiredValidation
) to prevent naming conflicts when multiple structs have fields with the same name.
- Use the generated validation:
|
|
Performance Highlights
- 5x to 50x faster than popular validation libraries
- Zero allocations across all validators
- Sub-3ns execution for simple validators (Required, GT, LT)
- Extended type support including maps, channels, and enums
📊 View detailed benchmark results →
Supported Validators
String Validators
govalid:required
- Required field validationgovalid:minlength=N
- Minimum string length (Unicode-aware)govalid:maxlength=N
- Maximum string length (Unicode-aware)govalid:email
- HTML5-compliant email validationgovalid:url
- HTTP/HTTPS URL validationgovalid:uuid
- RFC 4122 UUID validation
Numeric Validators
govalid:gt=N
- Greater than validationgovalid:gte=N
- Greater than or equal validationgovalid:lt=N
- Less than validationgovalid:lte=N
- Less than or equal validation
Collection Validators
govalid:minitems=N
- Minimum collection sizegovalid:maxitems=N
- Maximum collection size
General Validators
govalid:enum=val1,val2,val3
- Enum validation
Why govalid?
🔥 Performance First
- Zero allocations: No heap allocations during validation
- Compile-time optimization: Generated code is optimized by the Go compiler
- Minimal overhead: Direct field access with no reflection
🎯 Developer Experience
- Familiar syntax: Similar to popular validation libraries
- Type safety: Validation functions are generated with proper types
- Clear error messages: Descriptive validation error messages
🚀 Production Ready
- Extensive test coverage: Comprehensive unit and benchmark tests
- Battle tested: Used in production environments
- Maintained: Regular updates and improvements
Get Started
Ready to make your Go validation blazingly fast? Check out our Getting Started guide or browse the available validators.
Zero Allocations
All validators perform zero heap allocations, making them perfect for high-performance applications.
Blazing Fast
5x to 44x faster than reflection-based validators with sub-nanosecond performance for simple operations.
Type Safe
Generated validation functions ensure type safety and catch errors during code generation.
Simple Syntax
Familiar marker-based syntax similar to other validation libraries, easy to learn and use.
Comprehensive
Support for all common validation patterns plus unique features like CEL expressions, enum validation, and extended collection types.
Production Ready
Battle-tested with extensive benchmarks and used in production environments.
Quick Example
See how easy it is to add validation to your Go structs:
1. Define your struct with markers
type User struct {
// +govalid:required
// +govalid:minlength=2
Name string `json:"name"`
// +govalid:email
Email string `json:"email"`
// +govalid:gte=18
Age int `json:"age"`
}
2. Generate and use validation
# Generate validation code
govalid .
// Use generated validation
user := &User{
Name: "Alice",
Email: "alice@example.com",
Age: 25,
}
if err := ValidateUser(user); err != nil {
log.Fatal(err)
}
🚀 Exceptional Performance
govalid delivers 5x to 44x performance improvements with zero allocations
Ready to Get Started?
Install govalid and start validating your Go structs with zero allocations and maximum performance.