Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var JSONTagNameFunc = func(fld reflect.StructField) string { name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0] if name == "-" { return "" } return name }
JSONTagNameFunc extracts the field name from the `json` struct tag.
Functions ¶
This section is empty.
Types ¶
type CustomValidator ¶
type CustomValidator interface { // Tag returns the tag identifier used in struct field validation tags (e.g., `validate:"tag"`). Tag() string // Func returns the validator.Func that performs the validation logic. Func() validator.Func // Translation returns the translation text and an custom translation function for the custom validator. // To use the default translation, return an empty string and nil. Translation() (translation string, customFunc validator.TranslationFunc) }
CustomValidator defines the interface that custom validators must implement. It requires methods to return the validation tag, function, and translation details.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator wraps go-playground/validator with translation support for user-friendly error messages. Provides struct validation with internationalized error messages and custom validation rules.
func NewValidator ¶
func NewValidator(opts ...ValidatorOption) (*Validator, error)
NewValidator creates a configured Validator instance with English translations and custom options. Initializes the underlying validator, sets up English locale translation, and applies custom configurations.
Parameters:
- opts: Optional configuration functions for custom validators and settings
Returns:
- *Validator: Configured validator instance ready for use
- error: Initialization error if translator setup or custom options fail
Example:
// Basic validator with defaults validator, err := NewValidator() // Validator with JSON field names and custom validator validator, err := NewValidator( WithTagNameFunc(JSONTagNameFunc), WithCustomValidator(DateValidator{}), )
func (*Validator) Struct ¶
Struct validates the provided struct using the validator instance. This method is introduced for compatibility with validator v10, which expects a method named Struct to perform validation on structs.
It uses the same underlying validation logic as ValidateStruct, translating validation error messages using the provided translator instance.
Example:
err := v.Struct(myStruct) if err != nil { // Handle validation errors }
func (*Validator) ValidateStruct ¶
ValidateStruct validates the provided struct using the validator instance. It returns an error containing all validation errors with messages translated using the translator.
Example:
err := v.ValidateStruct(myStruct) if err != nil { // Handle validation errors }
type ValidatorOption ¶
type ValidatorOption func(*validator.Validate, ut.Translator) error
ValidatorOption defines a functional option for configuring the validator instance.
func WithCustomValidator ¶
func WithCustomValidator(cv CustomValidator) ValidatorOption
WithCustomValidator registers a custom validator along with its translation. It uses the CustomValidator interface to get the tag, function, and translation details.
func WithTagNameFunc ¶
func WithTagNameFunc(tagNameFunc func(fld reflect.StructField) string) ValidatorOption
WithTagNameFunc configures custom field name extraction for validation error messages. Allows using struct tag values (like json, yaml, db) instead of struct field names in error messages.
Parameters:
- tagNameFunc: Function that extracts field names from struct field tags
Returns:
- ValidatorOption: Configuration option for validator setup
Example:
// Use JSON tag names in error messages WithTagNameFunc(JSONTagNameFunc)