validation

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package validation contains composable, typed validation rules without reflection. Errors is the original field-message API; Report adds explicit input states, stable rule codes, nested paths, and application-defined rules.

Index

Constants

View Source
const (
	CodeRequired     = "required"
	CodeStringMin    = "string.min"
	CodeStringMax    = "string.max"
	CodeNumberMin    = "number.min"
	CodeNumberMax    = "number.max"
	CodeNumberFinite = "number.finite"
	CodeListMin      = "list.min"
	CodeListMax      = "list.max"
	CodeListUnique   = "list.unique"
)
View Source
const CodeInvalidConfiguration = "validation.configuration"

Variables

This section is empty.

Functions

This section is empty.

Types

type Errors

type Errors map[string][]string

func Check

func Check(fields ...Field) Errors

func (Errors) Empty

func (errors Errors) Empty() bool

func (Errors) Error

func (errors Errors) Error() string

type Field

type Field interface {
	// contains filtered or unexported methods
}

type Input

type Input[T any] struct {
	// contains filtered or unexported fields
}

Input is an explicitly classified value. Its zero value is Missing. Decoders remain application-owned and choose the state without reflection. Empty represents an input that contained no decodable value, such as an empty scalar form field. A decoded empty list is normally Present([]T{}).

func Empty

func Empty[T any]() Input[T]

func Missing

func Missing[T any]() Input[T]

func Null

func Null[T any]() Input[T]

func Present

func Present[T any](value T) Input[T]

func (Input[T]) Provided

func (input Input[T]) Provided() bool

Provided reports whether the input was present, including null and empty.

func (Input[T]) State

func (input Input[T]) State() State

func (Input[T]) Value

func (input Input[T]) Value() (T, bool)

Value returns a decoded value only for Present input.

type IntField

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

func Int

func Int(name string, value int) *IntField

func (*IntField) Max

func (field *IntField) Max(value int) *IntField

func (*IntField) Min

func (field *IntField) Min(value int) *IntField

type Issue

type Issue struct {
	Path    string
	Code    string
	Message string
}

Issue is returned by a Rule. Path is relative to the field being checked; an empty path refers to that field. Messages are plain text, never trusted HTML. Built-in rules do not include submitted values in messages.

type Number

type Number interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64
}

type Report

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

Report preserves validation order while providing a structured alternative to the legacy Errors map.

func Apply

func Apply[T any](path string, input Input[T], rules ...Rule[T]) Report

Apply evaluates rules for one field and returns fully qualified violations. Invalid field paths, nil rules, and malformed rule output are reported with CodeInvalidConfiguration and never silently ignored.

func Join

func Join(reports ...Report) Report

Join combines reports in argument order.

func (Report) Count

func (report Report) Count() int

func (Report) Empty

func (report Report) Empty() bool

func (Report) Error

func (report Report) Error() string

func (Report) For

func (report Report) For(path string) []Violation

For returns a defensive copy of violations for one fully qualified path.

func (Report) MarshalJSON

func (report Report) MarshalJSON() ([]byte, error)

MarshalJSON emits the ordered violation list, making Report safe to pass directly as structured HTTP error details.

func (Report) Messages

func (report Report) Messages() Errors

Messages converts a structured report to the established field-message map. Rule codes remain available through Violations.

func (Report) Prefix

func (report Report) Prefix(prefix string) Report

Prefix returns a report whose paths are nested beneath prefix. Invalid prefixes become an explicit configuration violation instead of being silently accepted.

func (Report) Violations

func (report Report) Violations() []Violation

Violations returns a defensive copy in rule evaluation order.

type Rule

type Rule[T any] interface {
	Validate(Input[T]) []Issue
}

Rule is an application-implementable, typed validation rule. Implementations return relative issues; Apply supplies the fully qualified field path.

func Each

func Each[T any](rules ...Rule[T]) Rule[[]T]

Each applies rules to every decoded list item and reports indexed paths.

func ListSize

func ListSize[T any](minimum, maximum int) Rule[[]T]

func ListUnique

func ListUnique[T comparable]() Rule[[]T]

func NumberRange

func NumberRange[T Number](minimum, maximum T) Rule[T]

func Required

func Required[T any]() Rule[T]

func StringLength

func StringLength(minimum, maximum int) Rule[string]

StringLength validates Unicode code points. Non-present states are left to state rules such as Required.

func When

func When[T any](condition bool, rules ...Rule[T]) Rule[T]

When conditionally applies rules. Built-in configuration errors are still reported when the condition is false.

type RuleFunc

type RuleFunc[T any] func(Input[T]) []Issue

RuleFunc adapts an ordinary function into a Rule.

func (RuleFunc[T]) Validate

func (rule RuleFunc[T]) Validate(input Input[T]) []Issue

type State

type State string

State records how an input appeared at the request boundary. It keeps omission and explicit null separate from an empty submitted value and a successfully decoded value.

const (
	StateMissing State = "missing"
	StateNull    State = "null"
	StateEmpty   State = "empty"
	StatePresent State = "present"
)

type StringField

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

func String

func String(name, value string) *StringField

String validates the original value. Blank values skip all rules unless Required is set.

func (*StringField) Email

func (field *StringField) Email() *StringField

func (*StringField) Max

func (field *StringField) Max(length int) *StringField

Max sets the maximum length in Unicode code points, including whitespace.

func (*StringField) Min

func (field *StringField) Min(length int) *StringField

Min sets the minimum length in Unicode code points, including whitespace.

func (*StringField) OneOf

func (field *StringField) OneOf(values ...string) *StringField

func (*StringField) Required

func (field *StringField) Required() *StringField

type Violation

type Violation struct {
	Path    string `json:"path"`
	Code    string `json:"code"`
	Message string `json:"message"`
}

Violation is a fully qualified, transport-safe validation result. Path and Code are stable machine-readable identifiers. Message is single-line plain text and still must be escaped by its eventual renderer.

Jump to

Keyboard shortcuts

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