Documentation
¶
Overview ¶
Package validate checks a struct against the rules in its validate tags, and says what's wrong in words for the person who filled in the form:
type PostInput struct {
Title string `json:"title" validate:"required,max=80"`
Email string `json:"email" validate:"required,email"`
}
err := validate.Struct(in) // Errors{"title": "title is required", ...}
The rules are those of github.com/go-playground/validator, and two of tug's own for uploads, a *multipart.FileHeader's: file_max, its size at most, and file_type, what it is, told from its first bytes:
Photo *multipart.FileHeader `form:"photo" validate:"required,file_max=2MB,file_type=image/png image/jpeg"`
Errors are keyed as their json tags name the fields, the way the client sent them, and a nested field by its path: "author.name", "items.0.price". A message names a field as a person reads it: by its label tag, `label:"email address"`, or else by its key made into words, "first_name" as "first name".
Messages are said in English, or in the language given, in which the messages and the fields' names are texts of a language's file: see package lang. tug's BindValid gives the request's. Rule adds a rule of the app's own, for a tag such as slug.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Rule ¶ added in v0.15.0
Rule adds a rule of the app's own, for a tag, as `validate:"slug"`: check reports whether a field's value keeps it, given the tag's parameter, "VN" for phone=VN, and message says what's wrong when it doesn't, in English, with :field for the field's name and :param for the parameter, as a language's file translates it:
validate.Rule("slug", func(s string, _ string) bool {
return slugPattern.MatchString(s)
}, ":field must be letters, digits and dashes")
T is the type of the fields the rule checks, or one they have the same kind as, as a type Slug string is a string, or an interface they have: a field of another type panics as it's checked, as a test finds. Rules are added as the app starts, as its routes are; one added again replaces the rule before, as an app's tests that make the app afresh add them again. A name the validator keeps for itself, as omitempty, panics.
Types ¶
type Errors ¶
Errors are what's wrong with a request's values: a message for each field that failed, by the field's name.
func (Errors) Add ¶
Add records message for field, unless the field already has one: the first thing wrong with a field is the one worth saying.