Help us shape the future of Go! Take the 2026 Go Developer Survey

validate

package
v0.39.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 13 Imported by: 0

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

func Rule[T any](name string, check func(value T, param string) bool, message string)

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.

func Struct

func Struct(v any, in ...lang.Words) error

Struct checks the struct v points to, or is, against its validate tags. It returns Errors when something is wrong, and nil when nothing is. The messages are in English, or in the language of in, one at most, as tug's BindValid has the request's.

func Texts added in v0.15.0

func Texts() []string

Texts lists what validate says, in English, as a language's file has the texts: its messages, and those of the rules Rule has added. The names of fields and of file types are texts too, which tug lang finds in the app's own Go.

Types

type Errors

type Errors map[string]string

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

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

Add records message for field, unless the field already has one: the first thing wrong with a field is the one worth saying.

func (Errors) Error

func (e Errors) Error() string

Error lists the messages, by field name.

func (Errors) First

func (e Errors) First() string

First returns the message of the field that sorts first, for a summary such as a 422's message.

func (Errors) Only

func (e Errors) Only(fields ...string) Errors

Only returns the errors of the fields named, and of the fields nested in them: "author" keeps "author.name" too.

Jump to

Keyboard shortcuts

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