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.
Types ¶
type RuleFunc ¶
RuleFunc is a custom validation rule. It returns an error message on failure, or "".
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator validates structs against `validate` struct tags.
func New ¶
func New() *Validator
New creates a Validator. The zero value is not usable; always use New().
func (*Validator) Register ¶
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 ¶
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.