validate

package
v0.1.36 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

pantry/validate/email.go

Package validate provides struct validation using struct tags with support for custom validators and internationalized error messages.

Basic usage:

type User struct {
    Name  string `validate:"required,min=2,max=50"`
    Email string `validate:"required,email"`
    Age   int    `validate:"min=18,max=120"`
}

v := validate.New()
if err := v.Struct(user); err != nil {
    for _, e := range err.(validate.Errors) {
        fmt.Printf("%s: %s\n", e.Field, e.Message)
    }
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterRule

func RegisterRule(name string, fn RuleFunc)

RegisterRule registers a rule on the default validator.

func SimpleEmailValid

func SimpleEmailValid(s string) bool

SimpleEmailValid is a light, readable server-side guardrail. It is not an RFC validator; it catches empty, missing '@', or no dot in the domain.

Limitation: This function intentionally rejects local-only addresses (e.g., "user@localhost", "admin@server") because they lack a dot in the domain portion. This is by design for production use cases where internet-routable email addresses are expected. For intranet or testing scenarios that need local domains, use a custom validator.

Note: For stricter validation (e.g., ACME registration where account recovery requires a valid email), see config.isValidEmail which adds RFC 5321 length limits and additional character restrictions.

func Struct

func Struct(s any) error

Struct validates a struct using the default validator.

func Var

func Var(value any, tag string) error

Var validates a variable using the default validator.

Types

type Error

type Error struct {
	Field   string
	Rule    string
	Param   string
	Value   any
	Message string
}

Error represents a validation error.

func (*Error) Error

func (e *Error) Error() string

type Errors

type Errors []*Error

Errors is a collection of validation errors.

func (Errors) Error

func (e Errors) Error() string

func (Errors) FieldErrors

func (e Errors) FieldErrors(field string) Errors

FieldErrors returns all errors for a specific field.

func (Errors) First

func (e Errors) First() *Error

First returns the first error or nil.

func (Errors) HasErrors

func (e Errors) HasErrors() bool

HasErrors returns true if there are any errors.

func (Errors) ToMap

func (e Errors) ToMap() map[string][]string

ToMap converts errors to a map of field -> messages.

type MessageProvider

type MessageProvider struct {
	// contains filtered or unexported fields
}

MessageProvider provides validation error messages with i18n support.

func DefaultMessages

func DefaultMessages() *MessageProvider

DefaultMessages returns a message provider with default English messages.

func MessagesForLocale

func MessagesForLocale(locale string) *MessageProvider

MessagesForLocale creates a message provider for a specific locale.

func NewMessageProvider

func NewMessageProvider() *MessageProvider

NewMessageProvider creates a new message provider.

func (*MessageProvider) AddMessage

func (m *MessageProvider) AddMessage(locale, key, message string)

AddMessage adds or updates a message for a locale.

func (*MessageProvider) Clone

func (m *MessageProvider) Clone() *MessageProvider

Clone creates a copy of the message provider.

func (*MessageProvider) Get

func (m *MessageProvider) Get(key, field, param string) string

Get retrieves a message for the current locale. It supports placeholders: {field} for field name, {param} for parameter.

func (*MessageProvider) RegisterBuiltinLocales

func (m *MessageProvider) RegisterBuiltinLocales()

RegisterBuiltinLocales registers all built-in locales.

func (*MessageProvider) RegisterLocale

func (m *MessageProvider) RegisterLocale(locale string, messages map[string]string)

RegisterLocale registers messages for a locale.

func (*MessageProvider) SetFallback

func (m *MessageProvider) SetFallback(locale string)

SetFallback sets the fallback locale.

func (*MessageProvider) SetLocale

func (m *MessageProvider) SetLocale(locale string)

SetLocale sets the current locale.

type Option

type Option func(*Validator)

Option configures the validator.

func WithMessages

func WithMessages(m *MessageProvider) Option

WithMessages sets a custom message provider.

func WithStopOnFirstError

func WithStopOnFirstError() Option

WithStopOnFirstError stops validation after the first error.

func WithTagName

func WithTagName(name string) Option

WithTagName sets a custom tag name (default: "validate").

type RuleFunc

type RuleFunc func(value any, param string, structValue reflect.Value) string

RuleFunc is a validation rule function. It receives the field value, the parameter (if any), and the full struct. Returns an error message key if validation fails, empty string if valid.

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator validates struct fields using tags.

func New

func New(opts ...Option) *Validator

New creates a new validator with default rules.

func (*Validator) RegisterRule

func (v *Validator) RegisterRule(name string, fn RuleFunc)

RegisterRule registers a custom validation rule.

func (*Validator) RegisterRuleFunc

func (v *Validator) RegisterRuleFunc(name string, fn func(value any) bool, messageKey string)

RegisterRuleFunc registers a simple validation function.

func (*Validator) SetMessages

func (v *Validator) SetMessages(m *MessageProvider)

SetMessages sets the message provider.

func (*Validator) Struct

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

Struct validates a struct using its validate tags.

func (*Validator) StructCtx

func (v *Validator) StructCtx(ctx map[string]any, s any) error

StructCtx validates a struct with additional context data.

func (*Validator) Var

func (v *Validator) Var(value any, tag string) error

Var validates a single variable.

Jump to

Keyboard shortcuts

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