govalid offers blazing fast, zero-allocation validation code generation for Go structs using markers. With performance up to 44 times faster than traditional methods, it ensures type safety by catching errors at generation time and supporting complex validation requirements seamlessly.
govalid is a powerful tool designed for Go developers, enabling the generation of type-safe validation code based on struct field markers. It provides a validation process that eliminates runtime overhead and reflection costs, ensuring optimal performance and faster validation.
Define Your Struct with Markers: Use built-in markers to specify validation rules directly within your struct definition.
//govalid:required
type Person struct {
Name string `json:"name"`
//govalid:email
Email string `json:"email"`
}
Generate Validation Code: Running the command will create the necessary validation code automatically.
govalid ./... ```
func main() {
p := &Person{Name: "John", Email: "invalid-email"}
if err := p.Validate(); err != nil {
log.Printf("Validation failed: %v", err)
}
}
Markers are a vital part of defining validation rules. Here’s a brief look at some of the available markers:
required: Ensures the field cannot be zero value.gt: Validates the field to be greater than a specified value.email: Checks for a correctly formatted email address.maxitems and minitems.govalid has demonstrated exceptional performance metrics in various validation scenarios:
| Validator | govalid | Other Validators | 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 |
For detailed benchmark results, refer to the benchmarking documentation.
govalid streamlines the validation process in Go projects significantly, ensuring effective performance and reliability while maintaining type safety. Perfect for developers looking to implement efficient validation frameworks without sacrificing performance.
No comments yet.
Sign in to be the first to comment.