Flexible, multi-strategy validation for Go structs with support for struct tags, JSON Schema, and custom interfaces.
📚 Full Documentation: https://rivaas.dev/docs/guides/validation/
- Installation - Get started
- User Guide - Learn validation strategies
- API Reference - Complete API docs
- Examples - Real-world patterns
- Troubleshooting - Common issues
- Multiple Validation Strategies - Struct tags, JSON Schema, custom interfaces
- Partial Validation - PATCH request support with presence tracking
- Thread-Safe - Safe for concurrent use
- Security - Built-in redaction, nesting limits, memory protection
- Structured Errors - Field-level errors with codes and metadata
- Extensible - Custom tags, validators, and error messages
go get rivaas.dev/validationRequires Go 1.25+
import "rivaas.dev/validation"
type User struct {
Email string `json:"email" validate:"required,email"`
Age int `json:"age" validate:"min=18"`
}
user := User{Email: "invalid", Age: 15}
if err := validation.Validate(ctx, &user); err != nil {
var verr *validation.Error
if errors.As(err, &verr) {
for _, fieldErr := range verr.Fields {
fmt.Printf("%s: %s\n", fieldErr.Path, fieldErr.Message)
}
}
}Use errors.Is(err, validation.ErrValidation) and the package's sentinel errors for programmatic checks (see Error Handling).
The package-level Validate and ValidatePartial use DefaultEngine, which is lazily initialized. You can replace it in tests (e.g. validation.DefaultEngine = validation.MustNew(...) and restore in defer). For test isolation or multiple engines, create an Engine with New or MustNew and use engine.Validate instead.
type User struct {
Email string `validate:"required,email"`
Age int `validate:"min=18,max=120"`
}func (u User) JSONSchema() (id, schema string) {
return "user-v1", `{
"type": "object",
"properties": {
"email": {"type": "string", "format": "email"},
"age": {"type": "integer", "minimum": 18}
},
"required": ["email"]
}`
}func (u *User) Validate() error {
if !strings.Contains(u.Email, "@") {
return errors.New("email must contain @")
}
return nil
}
// When using a pointer receiver, pass a pointer: validation.Validate(ctx, &user)- Basic Usage - Fundamentals
- Struct Tags - go-playground/validator syntax
- JSON Schema - Schema validation
- Custom Interfaces - Custom methods
- Partial Validation - PATCH requests
- Error Handling - Structured errors
- Security - Redaction and limits
Contributions are welcome! Please see the main repository for contribution guidelines.
Apache License 2.0 - see LICENSE for details.
Part of the Rivaas web framework ecosystem.