validation

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package validation defines the validation contract.

There is no reflection and there are no struct tags: every request type implements Validate and returns the errors per field. It is more verbose than `binding:"required"`, and deliberately so -- the message is written by whoever knows the domain, and the CLI generates the method skeleton along with the struct.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Email

func Email(e Errors, field, value string)

Email checks the shape only. Deliverability is proven by sending mail, never by a regular expression.

Whitespace is rejected rather than trimmed: an address with a space in it is almost always a paste accident, and silently trimming input hides the mistake from the person who made it.

func MaxLen

func MaxLen(e Errors, field, value string, n int)

MaxLen counts runes, not bytes.

func MinLen

func MinLen(e Errors, field, value string, n int)

MinLen counts runes, not bytes: a limit measured in bytes rejects valid input in any language that needs more than one byte per character.

func NotZero added in v0.10.0

func NotZero[T comparable](e Errors, field string, value T)

NotZero reports a value that was never filled in.

It is Required for everything that is not text. Required takes a string and the generator used to hand it a literal "" for an int, a date or an amount -- so every required field of those types failed validation with "is required" no matter what was sent, and the generated create endpoint could not be used at all. Found by audit.

A time.Time is asked rather than compared: a parsed "0001-01-01T00:00:00Z" carries a location the zero value does not, so == says they differ when they do not.

Bool has no meaningful zero to reject -- false is an answer, not an absence -- and the specification refuses `required` on a bool for that reason.

func Required

func Required(e Errors, field, value string)

Required rejects an empty or blank value.

Types

type Errors

type Errors map[string][]string

Errors maps a field to its messages. It serializes straight into the HTMX partial that re-renders the form with inline errors.

func (Errors) Add

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

Add appends a message to a field. It is a no-op on a nil map, so a caller that forgot to initialize the map does not panic in the middle of a request.

func (Errors) Any

func (e Errors) Any() bool

Any reports whether validation failed.

func (Errors) Error

func (e Errors) Error() string

Error renders the errors with fields in a stable order, so that logs and golden files do not change between runs.

func (Errors) First added in v0.11.1

func (e Errors) First() string

First returns one message, for a view that shows a single line.

A form that lists every error next to its field uses the map directly. One that shows a banner needs one sentence, and picking it at the call site means every template does it slightly differently.

The field order is not stable -- a map has none -- so this is for the case where any of them answers the question "what is wrong". When which field failed matters, read the map.

type Validatable

type Validatable interface {
	Validate() Errors
}

Validatable is implemented by every request type.

Jump to

Keyboard shortcuts

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