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: Compile-time validation function generation
- ⚡ High Performance: Up to 45x 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:
|
|
- Use the generated validation:
|
|
Performance Comparison
Validator | govalid | go-playground/validator | Improvement |
---|---|---|---|
Required | 1.914 | 85.82 | 44.8x faster |
36.80 | 630.4 | 17.1x faster | |
GT/LT | ~1.914 | ~61.48 | 32.1x faster |
MaxLength | 15.58 | 74.90 | 4.8x faster |
Enum | 2.223 | N/A (unique to govalid) | govalid exclusive |
All benchmarks show 0 allocations for govalid vs 0-5 allocations for competitors
Methodology: 10 runs per benchmark analyzed with benchstat
for statistical significance
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: Catch validation errors at compile time
- 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
4.8x to 44.2x faster than go-playground/validator with sub-nanosecond performance for simple operations.
Type Safe
Compile-time validation function generation ensures type safety and catches errors early.
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 enum validation.
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)
}
Performance Comparison
govalid consistently outperforms go-playground/validator across all validation types:
Validator | govalid | go-playground/validator | Improvement |
---|---|---|---|
Required | 1.96ns | 86.59ns | 44.2x faster |
38.15ns | 644.1ns | 16.9x faster | |
Numeric (GT/LT) | ~1.97ns | ~63ns | 32x faster |
String Length | ~14ns | ~75ns | 5x faster |
Ready to Get Started?
Install govalid and start validating your Go structs with zero allocations and maximum performance.