validate

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomValidator

type CustomValidator interface {
	Validate(value any) error
}

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 DBRow

type DBRow interface {
	Scan(dest ...any) error
}

DBRow is the minimal interface for scanning a single value from a query.

type Field

type Field struct {
	Name     string
	Value    any
	Rules    []*Rule
	Required bool
	Optional bool
}

Field represents a field to be validated

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) Date

func (fb *FieldBuilder) Date() *FieldBuilder

Date adds date 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) JSON

func (fb *FieldBuilder) JSON() *FieldBuilder

JSON adds JSON 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

func (*FieldBuilder) URL

func (fb *FieldBuilder) URL() *FieldBuilder

URL adds URL validation

func (*FieldBuilder) UUID

func (fb *FieldBuilder) UUID() *FieldBuilder

UUID adds UUID validation

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 Rule

type Rule struct {
	Name       string
	Validator  func(any) error
	Message    string
	StopOnFail bool
}

Rule represents a validation rule

type ValidationErrors

type ValidationErrors struct {
	Fields map[string][]string `json:"fields"`
}

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

type ValidationResult struct {
	Valid  bool              `json:"valid"`
	Errors map[string]string `json:"errors"`
}

ValidationResult represents the result of validation

func Struct

func Struct(s any) *ValidationResult

Struct validates a struct using struct tags

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

func (v *Validator) BindAndValidate(r *http.Request, val any) error

BindAndValidate decodes the request body and validates the struct.

func (*Validator) Validate

func (v *Validator) Validate(s any) error

Validate validates a struct.

func (*Validator) ValidateStruct

func (v *Validator) ValidateStruct(obj any, locale ...string) error

ValidateStruct validates a struct and returns structured ValidationErrors. It respects the locale if provided in the context (via i18n middleware).

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL