Documentation
¶
Overview ¶
Package validation turns struct-tag validation into user-friendly field errors. It wraps go-playground/validator so handlers can validate a request and get back a map of field name to a readable message rather than the library's raw error.
ValidatorService is the entry point, built with NewValidatorService. Validate runs the struct tags on a value and returns the per-field messages; field names are reported in snake_case so they match the JSON the client sent. Each built-in validation tag has a default message, and the service ships with the custom validators the platform needs — for example stellar_xdr, which checks that a field decodes as a Stellar transaction envelope.
The validator set is extensible. RegisterValidator adds a new tag with its own check and message, and RegisterErrorMessage overrides the wording for an existing tag, so callers can tailor validation without forking the package.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrorMessageFunc ¶
ErrorMessageFunc is a function that generates an error message for a validation error. It receives the field name and the validation parameter (if any).
type ValidatorService ¶
type ValidatorService struct {
// contains filtered or unexported fields
}
ValidatorService wraps the validator.Validate instance and provides custom validation functionality with user-friendly error messages.
func NewValidatorService ¶
func NewValidatorService() *ValidatorService
NewValidatorService creates a new validation service with default validators.
func (*ValidatorService) RegisterErrorMessage ¶
func (vs *ValidatorService) RegisterErrorMessage(tag string, errorMsgFn ErrorMessageFunc)
RegisterErrorMessage registers or updates an error message for a validation tag. This is useful when you want to customize error messages for built-in validators.
func (*ValidatorService) RegisterValidator ¶
func (vs *ValidatorService) RegisterValidator(tag string, validatorFn validator.Func, errorMsgFn ErrorMessageFunc) error
RegisterValidator registers a custom validator function with its error message. tag: the validation tag to use in struct tags (e.g., "stellar_xdr") validatorFn: the validation function errorMsgFn: the error message function (receives field name and param)