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
- type Errors
- type Field
- type Input
- type IntField
- type Issue
- type Number
- type Report
- func (report Report) Count() int
- func (report Report) Empty() bool
- func (report Report) Error() string
- func (report Report) For(path string) []Violation
- func (report Report) MarshalJSON() ([]byte, error)
- func (report Report) Messages() Errors
- func (report Report) Prefix(prefix string) Report
- func (report Report) Violations() []Violation
- type Rule
- func Each[T any](rules ...Rule[T]) Rule[[]T]
- func ListSize[T any](minimum, maximum int) Rule[[]T]
- func ListUnique[T comparable]() Rule[[]T]
- func NumberRange[T Number](minimum, maximum T) Rule[T]
- func Required[T any]() Rule[T]
- func StringLength(minimum, maximum int) Rule[string]
- func When[T any](condition bool, rules ...Rule[T]) Rule[T]
- type RuleFunc
- type State
- type StringField
- type Violation
Constants ¶
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" )
const CodeInvalidConfiguration = "validation.configuration"
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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{}).
type Issue ¶
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 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 ¶
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 (Report) MarshalJSON ¶
MarshalJSON emits the ordered violation list, making Report safe to pass directly as structured HTTP error details.
func (Report) Messages ¶
Messages converts a structured report to the established field-message map. Rule codes remain available through Violations.
func (Report) Prefix ¶
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 ¶
Violations returns a defensive copy in rule evaluation order.
type Rule ¶
Rule is an application-implementable, typed validation rule. Implementations return relative issues; Apply supplies the fully qualified field path.
func ListUnique ¶
func ListUnique[T comparable]() Rule[[]T]
func NumberRange ¶
func StringLength ¶
StringLength validates Unicode code points. Non-present states are left to state rules such as Required.
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.
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.