Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Validation

Go Reference Go Report Card Coverage Go Version License

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/

Documentation

Features

  • 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

Installation

go get rivaas.dev/validation

Requires Go 1.25+

Quick Start

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.

Validation Strategies

1. Struct Tags

type User struct {
    Email string `validate:"required,email"`
    Age   int    `validate:"min=18,max=120"`
}

2. JSON Schema

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"]
    }`
}

3. Custom Interfaces

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)

Learn More

Contributing

Contributions are welcome! Please see the main repository for contribution guidelines.

License

Apache License 2.0 - see LICENSE for details.


Part of the Rivaas web framework ecosystem.