valid

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package valid wraps github.com/go-playground/validator/v10 behind a minimal Validator interface, returning xerrors-typed errors with stable codes.

The backend library is fully hidden — callers interact only with Validator and *xerrors.Err. Swapping the backend in the future requires no API changes.

Usage:

v := valid.New()
if err := v.Struct(req); err != nil {
    // err is *xerrors.Err with Code() == xerrors.ErrInvalidInput
    // err.Fields()["field"] contains the failing field name
}

// Spanish messages
v := valid.New(valid.WithMessageProvider(valid.SpanishMessages))

Struct tags follow go-playground/validator conventions:

type CreateUserReq struct {
    Email string `json:"email" validate:"required,email"`
    Age   int    `json:"age"   validate:"min=18"`
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FieldLevel

type FieldLevel interface {
	// Field returns the reflect.Value of the field being validated.
	Field() reflect.Value
	// Param returns the tag parameter, if any.
	// For validate:"mytag=value", Param() returns "value"; otherwise "".
	Param() string
	// FieldName returns the field name used for error messages (json tag or Go name).
	FieldName() string
}

FieldLevel provides access to the field being validated. It is passed to custom validator functions registered via WithCustomValidator.

type MessageProvider

type MessageProvider interface {
	Message(field, tag, param string) string
}

MessageProvider maps a validation failure to a human-readable message.

  • field: the struct field name (e.g. "Email")
  • tag: the failing validation rule (e.g. "required", "email", "min")
  • param: the rule parameter if any (e.g. "18" for min=18), or "" if none
var DefaultMessages MessageProvider = defaultMessages{}

DefaultMessages is the built-in English message provider. Used automatically when no WithMessageProvider option is given.

var SpanishMessages MessageProvider = spanishMessages{}

SpanishMessages is an opt-in Spanish message provider.

func OverrideProvider

func OverrideProvider(handlers map[string]func(field, param string) string, base MessageProvider) MessageProvider

OverrideProvider returns a MessageProvider that resolves messages from handlers for specific tags, delegating to base for any tag not in handlers.

Use it to supply messages for custom validators or to override individual tags:

p := valid.OverrideProvider(
    map[string]func(field, param string) string{
        "strongpassword": func(field, _ string) string {
            return fmt.Sprintf("field '%s' must contain a letter, digit, and symbol", field)
        },
    },
    valid.DefaultMessages,
)

type Option

type Option func(*config)

Option configures a Validator.

func WithCustomValidator

func WithCustomValidator(tag string, fn func(FieldLevel) bool) Option

WithCustomValidator registers a custom validation tag and its function. The tag can then be used in struct tags: validate:"mytag" or validate:"mytag=param". Panics if registration fails (empty tag or tag that conflicts with a built-in).

func WithMessageProvider

func WithMessageProvider(mp MessageProvider) Option

WithMessageProvider sets a custom MessageProvider. Default: DefaultMessages (English).

type Validator

type Validator interface {
	// Struct validates v and returns a *xerrors.Err if validation fails.
	// Returns nil if v is valid.
	// Returns ErrInvalidInput for field constraint failures (first error only).
	// Returns ErrInternal if v is not a struct.
	Struct(v any) error
}

Validator validates structs using struct tags.

func New

func New(opts ...Option) Validator

New returns a Validator. Without options, DefaultMessages (English) is used. Field names in error context use the json struct tag when available, falling back to the Go field name.

Jump to

Keyboard shortcuts

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