Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTagMandatory is returned when a translation is registered without a tag. ErrTagMandatory = errors.New("Tag is mandatory") // ErrTextMandatory is returned when a translation is registered without text. ErrTextMandatory = errors.New("Text is mandatory") // ErrLocaleMandatory is returned when a translation is registered without a locale. ErrLocaleMandatory = errors.New("Locale is mandatory") )
Functions ¶
func ErrorsToMap ¶
func ErrorsToMap(errors []ValidationErrors) map[string]string
ErrorsToMap converts a slice of ValidationErrors into a map where the key is the field namespace and the value is the human-readable error message.
func WithAllLocales ¶
func WithAllLocales() configFunc
WithAllLocales configures the Valid8 instance to support all available locales.
func WithLocales ¶
func WithLocales(locales ...Locale) configFunc
WithLocales configures the Valid8 instance to support a specific set of locales. English (EN) is always registered by default.
Types ¶
type Locale ¶
type Locale string
Locale represents a language identifier used for localized validation messages.
const ( // EN represents the English locale. EN Locale = "en" // PT_BR represents the Brazilian Portuguese locale. PT_BR Locale = "pt_BR" // ES represents the Spanish locale. ES Locale = "es" // FR represents the French locale. FR Locale = "fr" // DE represents the German locale. DE Locale = "de" // JA represents the Japanese locale. JA Locale = "ja" // ZH represents the Chinese (Simplified) locale. ZH Locale = "zh" // ZHTW represents the Chinese (Traditional) locale. ZHTW Locale = "zh_TW" )
type Translation ¶ added in v1.1.0
type Translation struct {
// IgnoreFallback if true, prevents the translation from being registered for the default English (EN) locale
// if the primary Locale is different.
IgnoreFallback bool
// Text is the localized error message template (e.g., "{0} is invalid").
Text string
// IgnoreOverride if true, prevents the translation from overriding an existing one for the same tag and locale.
IgnoreOverride bool
// Locale is the target language for this translation.
Locale Locale
// Tag is the validation tag identifier (e.g., "required", "email", "custom_tag").
Tag string
// TranslationFn is the function responsible for generating the localized error message.
// This field is optional; if not provided, it defaults to an internal translation function
// that retrieves the message from the universal-translator using the specified Tag.
TranslationFn validator.TranslationFunc
}
Translation defines the configuration for a custom validation tag translation.
type Valid8 ¶
type Valid8 struct {
// Validator is the underlying go-playground/validator instance used for validation.
Validator *validator.Validate
// contains filtered or unexported fields
}
Valid8 is the main structure for handling multi-language struct validation.
func New ¶
func New(opts ...configFunc) *Valid8
New creates and initializes a new Valid8 instance with the provided configuration options.
func (*Valid8) RegisterTranslation ¶ added in v1.1.0
func (v *Valid8) RegisterTranslation(translation Translation) error
RegisterTranslation registers a custom translation for a specific locale. By default, it also registers the translation for the English (EN) locale as a fallback, unless IgnoreFallback is set to true. This ensures that the custom validation tag has a meaningful message even if the requested language is not available.
type ValidationErrors ¶
type ValidationErrors struct {
validator.FieldError
// contains filtered or unexported fields
}
ValidationErrors wraps a validator.FieldError and its corresponding translated message.
func (*ValidationErrors) Message ¶
func (v *ValidationErrors) Message() string
Message returns the translated, human-readable error message.
func (*ValidationErrors) Namespace ¶
func (v *ValidationErrors) Namespace() string
Namespace returns the field namespace (e.g., "User.Email") in lowercase.