govalid

High-performance, type-safe validation library for Go with zero allocations

govalid

High-performance, type-safe validation library for Go with zero allocations

GitHub Go Report Card License: MIT

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

1
go install github.com/sivchari/govalid/cmd/govalid@latest

Basic Usage

  1. Define your struct with markers:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package main

type User struct {
    // +govalid:required
    Name string `json:"name"`
    
    // +govalid:email
    Email string `json:"email"`
    
    // +govalid:gte=18
    Age int `json:"age"`
    
    // +govalid:maxlength=100
    Bio string `json:"bio"`
}
  1. Generate validation code:
1
govalid ./...
  1. Use the generated validation:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
func main() {
    user := &User{
        Name:  "Alice",
        Email: "alice@example.com",
        Age:   25,
        Bio:   "Software developer",
    }
    
    if err := ValidateUser(user); err != nil {
        log.Fatal(err)
    }
    
    fmt.Println("User is valid!")
}

Performance Comparison

Validatorgovalidgo-playground/validatorImprovement
Required1.91485.8244.8x faster
Email36.80630.417.1x faster
GT/LT~1.914~61.4832.1x faster
MaxLength15.5874.904.8x faster
Enum2.223N/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 validation
  • govalid:minlength=N - Minimum string length (Unicode-aware)
  • govalid:maxlength=N - Maximum string length (Unicode-aware)
  • govalid:email - HTML5-compliant email validation
  • govalid:url - HTTP/HTTPS URL validation
  • govalid:uuid - RFC 4122 UUID validation

Numeric Validators

  • govalid:gt=N - Greater than validation
  • govalid:gte=N - Greater than or equal validation
  • govalid:lt=N - Less than validation
  • govalid:lte=N - Less than or equal validation

Collection Validators

  • govalid:minitems=N - Minimum collection size
  • govalid: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.

Get Started → View on GitHub →

🚀

Zero Allocations

All validators perform zero heap allocations, making them perfect for high-performance applications.

0 B/op 0 allocs/op

Blazing Fast

4.8x to 44.2x faster than go-playground/validator with sub-nanosecond performance for simple operations.

1.96ns required 44.2x faster
🔒

Type Safe

Compile-time validation function generation ensures type safety and catches errors early.

Compile-time Type safe
📝

Simple Syntax

Familiar marker-based syntax similar to other validation libraries, easy to learn and use.

// +govalid:required
🎯

Comprehensive

Support for all common validation patterns plus unique features like enum validation.

13 validators Enum support
🔧

Production Ready

Battle-tested with extensive benchmarks and used in production environments.

Battle tested Production ready

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:

Validatorgovalidgo-playground/validatorImprovement
Required1.96ns86.59ns44.2x faster
Email38.15ns644.1ns16.9x faster
Numeric (GT/LT)~1.97ns~63ns32x faster
String Length~14ns~75ns5x faster

View Detailed Benchmarks

Ready to Get Started?

Install govalid and start validating your Go structs with zero allocations and maximum performance.

Get Started View Validators Benchmarks GitHub