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 ¶
- func RegisterRule(name string, fn RuleFunc)
- func SimpleEmailValid(s string) bool
- func Struct(s any) error
- func Var(value any, tag string) error
- type Error
- type Errors
- type MessageProvider
- func (m *MessageProvider) AddMessage(locale, key, message string)
- func (m *MessageProvider) Clone() *MessageProvider
- func (m *MessageProvider) Get(key, field, param string) string
- func (m *MessageProvider) RegisterBuiltinLocales()
- func (m *MessageProvider) RegisterLocale(locale string, messages map[string]string)
- func (m *MessageProvider) SetFallback(locale string)
- func (m *MessageProvider) SetLocale(locale string)
- type Option
- type RuleFunc
- type Validator
- func (v *Validator) RegisterRule(name string, fn RuleFunc)
- func (v *Validator) RegisterRuleFunc(name string, fn func(value any) bool, messageKey string)
- func (v *Validator) SetMessages(m *MessageProvider)
- func (v *Validator) Struct(s any) error
- func (v *Validator) StructCtx(ctx map[string]any, s any) error
- func (v *Validator) Var(value any, tag string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterRule ¶
RegisterRule registers a rule on the default validator.
func SimpleEmailValid ¶
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.
Types ¶
type Errors ¶
type Errors []*Error
Errors is a collection of validation errors.
func (Errors) FieldErrors ¶
FieldErrors returns all errors for a specific field.
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 ¶
WithTagName sets a custom tag name (default: "validate").
type RuleFunc ¶
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 (*Validator) RegisterRule ¶
RegisterRule registers a custom validation rule.
func (*Validator) RegisterRuleFunc ¶
RegisterRuleFunc registers a simple validation function.
func (*Validator) SetMessages ¶
func (v *Validator) SetMessages(m *MessageProvider)
SetMessages sets the message provider.