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

validation

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package validation checks request fields beyond what the API contract states: ogen has already decoded the body and applied the contract's constraints, and a handler copies the fields it checks further into a local struct tagged for go-playground/validator. Errors name a field by its json tag, the name the contract and the client use. A domain module that refuses an input on a rule of its own returns an Error, which a handler answers with a 422 naming the field.

Index

Constants

View Source
const (
	PasswordMinLength = 8
	PasswordMaxLength = 256
)

The length of a password, in characters (NIST SP 800-63B counts characters, not bytes). Every character is allowed, spaces and non-ASCII included: length is the rule, composition is not.

Variables

This section is empty.

Functions

func AccountEmail

func AccountEmail(s string) bool

AccountEmail reports whether s is an address a person can receive the account's mail at (the `account_email` tag): an ASCII dot-atom local part and a domain of at least two labels of letters, digits and inner hyphens (punycode included), whose top-level label is not all digits. It is stricter than RFC 5322 on purpose: quoted local parts, address literals, IP addresses and dotless domains are valid syntax that no public mailbox uses.

func Check

func Check(s any, message func(validator.FieldError) string) (map[string][]string, error)

Check checks s and returns its failures as field errors, json name to messages, or nil when s is valid. message words one failure. An error means s cannot be validated at all (not a struct, a bad tag): a bug in the caller, never the request's fault.

func Message

func Message(err error) string

Message is the text to show a user for a domain error: the message of the refusal in its chain, otherwise the error's own text, which is already written for the user where the error is not a refusal (emailrender's ErrInvalidContent carries its reason).

func PasswordLength

func PasswordLength(s string) bool

PasswordLength reports whether s is valid UTF-8 of an acceptable length (the `password_length` tag).

func PasswordNotBlank

func PasswordNotBlank(s string) bool

PasswordNotBlank reports whether s has a character that is not white space (the `password_not_blank` tag). Spaces inside or around a password stay allowed and are kept as sent: sign-in compares the password as typed, so trimming it here would change it.

func Struct

func Struct(s any) error

Struct checks s against its validate tags. A failure is a validator.ValidationErrors whose Field() is the json name.

Types

type Error

type Error struct {
	Field   string
	Message string
}

Error is a refused input of a domain module: Field names the request field and Message is what to show for it, in the instance locale. A handler answers it with a 422 that carries the field.

func (*Error) Error

func (e *Error) Error() string

type RefusalError

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

RefusalError is a sentinel domain error that carries the text to show a user. Error() is for logs and carries the package prefix ("templates: …"), so it is never shown; Message is the i18n text, looked up when it is asked for because the locale is only set once the process boots. Callers still match with errors.Is.

func Refuse

func Refuse(text, key string) *RefusalError

Refuse makes the sentinel for a refusal: text is its Error() and key the i18n message a handler shows.

func (*RefusalError) Error

func (r *RefusalError) Error() string

func (*RefusalError) Message

func (r *RefusalError) Message() string

Message is the refusal's text in the instance locale.

func (*RefusalError) With

func (r *RefusalError) With(detail string) error

With is the refusal for one input: the sentinel's message followed by detail, which says what is wrong with it. errors.Is still matches the sentinel.

Jump to

Keyboard shortcuts

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