Documentation
¶
Index ¶
- type CustomValidator
- type DBExecutor
- type DBRow
- type Field
- type FieldBuilder
- func (fb *FieldBuilder) Alpha() *FieldBuilder
- func (fb *FieldBuilder) AlphaNumeric() *FieldBuilder
- func (fb *FieldBuilder) Custom(validator func(any) error, message string) *FieldBuilder
- func (fb *FieldBuilder) Date() *FieldBuilder
- func (fb *FieldBuilder) DateTime() *FieldBuilder
- func (fb *FieldBuilder) Email() *FieldBuilder
- func (fb *FieldBuilder) In(values ...any) *FieldBuilder
- func (fb *FieldBuilder) Integer() *FieldBuilder
- func (fb *FieldBuilder) JSON() *FieldBuilder
- func (fb *FieldBuilder) Max(max float64) *FieldBuilder
- func (fb *FieldBuilder) MaxLength(max int) *FieldBuilder
- func (fb *FieldBuilder) Min(min float64) *FieldBuilder
- func (fb *FieldBuilder) MinLength(min int) *FieldBuilder
- func (fb *FieldBuilder) NotIn(values ...any) *FieldBuilder
- func (fb *FieldBuilder) Numeric() *FieldBuilder
- func (fb *FieldBuilder) OneOf(values ...string) *FieldBuilder
- func (fb *FieldBuilder) Optional() *FieldBuilder
- func (fb *FieldBuilder) Password() *FieldBuilder
- func (fb *FieldBuilder) Pattern(pattern string) *FieldBuilder
- func (fb *FieldBuilder) Phone() *FieldBuilder
- func (fb *FieldBuilder) Required() *FieldBuilder
- func (fb *FieldBuilder) URL() *FieldBuilder
- func (fb *FieldBuilder) UUID() *FieldBuilder
- type MessageFormatter
- type Rule
- type ValidationErrors
- type ValidationResult
- type Validator
- type ValidatorOption
- type ValidatorSet
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CustomValidator ¶
CustomValidator interface for custom validators (renamed to avoid conflict)
type DBExecutor ¶
type DBExecutor interface {
// QueryRow executes a query expected to return at most one row.
// The query must accept a single text argument and return a single integer
// (the count). This is sufficient for EXISTS and UNIQUE checks.
QueryRow(ctx context.Context, sql string, args ...any) DBRow
}
DBExecutor is the minimal interface the validator needs to run DB-backed rules. It is satisfied by *sql.DB, *database.DB, or any custom adapter.
Migration from the old API:
// Before: validate.New(db) // After: validate.New(validate.WithDB(dbAdapter)) // or simply: validate.New() // no DB rules (exists/unique will be skipped)
type FieldBuilder ¶
type FieldBuilder struct {
// contains filtered or unexported fields
}
FieldBuilder provides fluent interface for building field validations
func (*FieldBuilder) Alpha ¶
func (fb *FieldBuilder) Alpha() *FieldBuilder
Alpha adds alphabetic validation
func (*FieldBuilder) AlphaNumeric ¶
func (fb *FieldBuilder) AlphaNumeric() *FieldBuilder
AlphaNumeric adds alphanumeric validation
func (*FieldBuilder) Custom ¶
func (fb *FieldBuilder) Custom(validator func(any) error, message string) *FieldBuilder
Custom adds custom validation
func (*FieldBuilder) DateTime ¶
func (fb *FieldBuilder) DateTime() *FieldBuilder
DateTime adds datetime validation
func (*FieldBuilder) Email ¶
func (fb *FieldBuilder) Email() *FieldBuilder
Email adds email validation
func (*FieldBuilder) In ¶
func (fb *FieldBuilder) In(values ...any) *FieldBuilder
In adds enum validation
func (*FieldBuilder) Integer ¶
func (fb *FieldBuilder) Integer() *FieldBuilder
Integer adds integer validation
func (*FieldBuilder) Max ¶
func (fb *FieldBuilder) Max(max float64) *FieldBuilder
Max adds maximum value validation
func (*FieldBuilder) MaxLength ¶
func (fb *FieldBuilder) MaxLength(max int) *FieldBuilder
MaxLength adds maximum length validation
func (*FieldBuilder) Min ¶
func (fb *FieldBuilder) Min(min float64) *FieldBuilder
Min adds minimum value validation
func (*FieldBuilder) MinLength ¶
func (fb *FieldBuilder) MinLength(min int) *FieldBuilder
MinLength adds minimum length validation
func (*FieldBuilder) NotIn ¶
func (fb *FieldBuilder) NotIn(values ...any) *FieldBuilder
NotIn adds negative enum validation
func (*FieldBuilder) Numeric ¶
func (fb *FieldBuilder) Numeric() *FieldBuilder
Numeric adds numeric validation
func (*FieldBuilder) OneOf ¶
func (fb *FieldBuilder) OneOf(values ...string) *FieldBuilder
OneOf adds validation that field must be one of the specified values
func (*FieldBuilder) Optional ¶
func (fb *FieldBuilder) Optional() *FieldBuilder
Optional marks the field as optional
func (*FieldBuilder) Password ¶
func (fb *FieldBuilder) Password() *FieldBuilder
Password adds password validation (at least 8 chars, uppercase, lowercase, number, special)
func (*FieldBuilder) Pattern ¶
func (fb *FieldBuilder) Pattern(pattern string) *FieldBuilder
Pattern adds regex pattern validation
func (*FieldBuilder) Phone ¶
func (fb *FieldBuilder) Phone() *FieldBuilder
Phone adds phone number validation
func (*FieldBuilder) Required ¶
func (fb *FieldBuilder) Required() *FieldBuilder
Required marks the field as required
type MessageFormatter ¶
type MessageFormatter func(fe validator.FieldError, locale ...string) string
MessageFormatter is a function that formats a field error into a human-readable string. It can optionally take a locale for i18n support.
type ValidationErrors ¶
ValidationErrors holds structured field validation errors.
func NewValidationErrors ¶
func NewValidationErrors() *ValidationErrors
NewValidationErrors creates a new ValidationErrors.
func (*ValidationErrors) Add ¶
func (ve *ValidationErrors) Add(field string, message string)
Add adds an error message for the given field.
func (*ValidationErrors) Error ¶
func (ve *ValidationErrors) Error() string
Error implements the error interface.
func (*ValidationErrors) HasErrors ¶
func (ve *ValidationErrors) HasErrors() bool
HasErrors returns true if there are any validation errors.
type ValidationResult ¶
ValidationResult represents the result of validation
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator wraps go-playground/validator with custom configurations.
func New ¶
func New(opts ...ValidatorOption) *Validator
New creates a new Validator. Pass options to configure it:
v := validate.New(validate.WithDB(dbAdapter), validate.WithMessageFormatter(i18nFn))
func (*Validator) BindAndValidate ¶
BindAndValidate decodes the request body and validates the struct.
type ValidatorOption ¶
type ValidatorOption func(*Validator)
ValidatorOption configures the Validator.
func WithCustomRule ¶
func WithCustomRule(tag string, fn validator.Func) ValidatorOption
WithCustomRule registers a custom validation rule.
func WithDB ¶
func WithDB(db DBExecutor) ValidatorOption
WithDB attaches a DB executor for database-backed validation rules (exists, unique). Without this, those rules silently pass.
func WithMessageFormatter ¶
func WithMessageFormatter(formatter MessageFormatter) ValidatorOption
WithMessageFormatter overrides the default English error message formatter. Useful for i18n integration.
type ValidatorSet ¶
type ValidatorSet struct {
// contains filtered or unexported fields
}
ValidatorSet represents a collection of validation rules
func NewValidatorSet ¶
func NewValidatorSet() *ValidatorSet
NewValidatorSet creates a new validator set
func (*ValidatorSet) Field ¶
func (vs *ValidatorSet) Field(name string, value any) *FieldBuilder
Field adds a field to be validated
func (*ValidatorSet) Validate ¶
func (vs *ValidatorSet) Validate() *ValidationResult
Validate runs all validations