Struct validation backed by go-playground/validator/v10 with xerrors integration and pluggable i18n message providers. What's included: - Validator interface with Struct(v any) error method - New(...Option) constructor with WithMessageProvider functional option - MessageProvider interface for i18n; DefaultMessages (EN) and SpanishMessages (ES) built in - ValidationErrors mapped to xerrors.ErrInvalidInput with field and tag context keys - InvalidValidationError (non-struct input) mapped to xerrors.ErrInternal - Full playground.ValidationErrors attached via WithError for callers needing all failures Tested-via: todo-api POC integration Reviewed-against: docs/adr/
32 lines
826 B
Go
32 lines
826 B
Go
// Package valid provides struct validation backed by [github.com/go-playground/validator/v10].
|
|
//
|
|
// Create a validator with [New]:
|
|
//
|
|
// v := valid.New()
|
|
//
|
|
// Validate a struct:
|
|
//
|
|
// type CreateUserRequest struct {
|
|
// Name string `validate:"required"`
|
|
// Email string `validate:"required,email"`
|
|
// Age int `validate:"min=18,max=120"`
|
|
// }
|
|
//
|
|
// err := v.Struct(req)
|
|
// if err != nil {
|
|
// // err is a *xerrors.Err with code ErrInvalidInput.
|
|
// // Use errors.As to inspect it.
|
|
// }
|
|
//
|
|
// Use Spanish messages:
|
|
//
|
|
// v := valid.New(valid.WithMessageProvider(valid.SpanishMessages))
|
|
//
|
|
// Use a custom message provider:
|
|
//
|
|
// type myMessages struct{}
|
|
// func (m myMessages) Message(field, tag, param string) string { ... }
|
|
//
|
|
// v := valid.New(valid.WithMessageProvider(myMessages{}))
|
|
package valid
|