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/
2.0 KiB
2.0 KiB
Changelog
All notable changes to this module will be documented in this file.
The format is based on Keep a Changelog, and this module adheres to Semantic Versioning.
0.9.0 - 2026-03-18
Added
Validatorinterface with a singleStruct(v any) errormethod for struct tag validation.New(opts ...Option) Validatorconstructor; returns an English-language validator by default.Optionfunctional-option type for configuring aValidatorat construction time.WithMessageProvider(mp MessageProvider) Optionoption to replace the built-in message provider.MessageProviderinterface withMessage(field, tag, param string) string; implement to supply custom or localised messages.DefaultMessages(MessageProvider): built-in English messages forrequired,email,min,max, and a generic fallback.SpanishMessages(MessageProvider): opt-in Spanish translations for the same tag set.- Error behaviour: a failing struct field returns
*xerrors.Errwith codeErrInvalidInput; structured context keys"field"and"tag"are attached viaWithContext. The rawplayground.ValidationErrorsis chained viaWithErrorfor callers that need all failures. - Error behaviour: passing a non-struct value returns
*xerrors.Errwith codeErrInternal(wrappingplayground.InvalidValidationError). - Only the first failing field is surfaced per
Structcall; the full error slice is preserved in the error chain.
Design Notes
go-playground/validator/v10is used as the backend but is entirely hidden behind theValidatorinterface; no playground types appear in the public API.MessageProviderdecouples human-readable text from validation logic, enabling i18n without any changes to the core package.- A single
*playground.Validateinstance is created perValidator(not per call) to amortise the cost of reflection-based type caching.