Documentation
¶
Overview ¶
Package validator provides a structured validation adapter using the go-playground/validator library with English translations for error messages.
The package wraps the underlying validator to provide a simple API that returns validation results as a boolean flag and a map of field-specific error messages. It also provides a convenience method to convert validation failures to a single error with formatted field descriptions.
Example usage:
adapter := validator.New()
ok, details := adapter.Validate(myStruct)
if !ok {
// details contains field -> error message mappings
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
var (
Default = New()
)
Default is a globally available Adapter instance for quick validation without explicit initialization. It is safe for concurrent use.
Example usage:
ok, details := validator.Default.Validate(myStruct)
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter struct {
// contains filtered or unexported fields
}
Adapter provides validation functionality with translated error messages. It wraps the go-playground/validator library and handles translation of validation errors into user-friendly messages.
func New ¶
func New() Adapter
New creates a new Adapter instance with English translation support. The returned Adapter is safe for concurrent use.
func (Adapter) Validate ¶
Validate checks if the provided value passes all validation rules. It returns true and nil details if validation succeeds, or false and a map of field names to error messages if validation fails. The value should be a struct with validator tags defined on its fields. Returns an error in the details map under "#validator" key if an unexpected error occurs during validation.
func (Adapter) ValidateToError ¶
ValidateToError validates the provided value and returns an error if validation fails. Returns nil if validation succeeds. The error message contains semicolon-separated field -> error message pairs for all validation failures.