validation

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package validation provides struct-tag-driven input validation for OniWorks.

Rules are declared as `validate:"required,min=3,max=255,email"` struct tags. The validator is allocation-light: rule lists are cached per type.

Failure messages can be customized at two levels:

  • Per validator: v.SetMessage("min", "{field} needs at least {param} characters")
  • Per field, via a `validate_msg` struct tag of comma-separated rule=message pairs: `validate_msg:"required=Please enter your name"`. Because pairs are comma-separated, messages themselves must not contain commas.

Field-tag messages take precedence over SetMessage overrides, which take precedence over the built-in English messages. Templates may use the {field} and {param} placeholders.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDefault

func SetDefault(v *Validator)

SetDefault replaces the package-level default Validator.

func Validate

func Validate(s any) error

Validate is a package-level shortcut using the default Validator.

Types

type Errors

type Errors map[string][]string

Errors holds validation failures keyed by field name.

func (Errors) Add

func (e Errors) Add(field, msg string)

Add appends a message for a field.

func (Errors) Any

func (e Errors) Any() bool

Any reports whether there are any validation errors.

func (Errors) Error

func (e Errors) Error() string

Error implements the error interface; it prints all field errors.

type RuleFunc

type RuleFunc func(value any, param string) string

RuleFunc is a custom validation rule. It returns an error message on failure, or "".

type Rules

type Rules map[string]string

Rules is a map of field name → rule string for use with ValidateMap.

type Validator

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

Validator validates structs against `validate` struct tags.

func Default

func Default() *Validator

Default returns the package-level default Validator.

func New

func New() *Validator

New creates a Validator. The zero value is not usable; always use New().

func (*Validator) Register

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

Register adds a custom validation rule.

v.Register("phone", func(val any, _ string) string {
    s, _ := val.(string)
    if !phoneRe.MatchString(s) { return "must be a valid phone number" }
    return ""
})

func (*Validator) SetMessage

func (v *Validator) SetMessage(rule, template string)

SetMessage overrides the built-in failure message for a rule on this Validator. The template may contain the placeholders {field} (the field name, e.g. "email" or "address.city") and {param} (the rule parameter, e.g. "3" for min=3):

v.SetMessage("min", "{field} must be at least {param} characters")

SetMessage applies to built-in and custom (Register-ed) rules alike. Per-field `validate_msg` struct tags take precedence over SetMessage. It is safe to call concurrently with Validate, but is intended for startup-time configuration.

func (*Validator) Validate

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

Validate validates s (a struct pointer) against its `validate` tags. Returns nil if all fields pass. Returns Errors (implementing error) on failure.

func (*Validator) ValidateMap

func (v *Validator) ValidateMap(data map[string]any, rules Rules) Errors

ValidateMap validates a map[string]any against a rules map.

errs := v.ValidateMap(data, validation.Rules{
    "email": "required,email",
    "age":   "required,min=18",
})

Jump to

Keyboard shortcuts

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