Documentation
¶
Overview ¶
Package valid provides declarative, type-safe struct validation. A field is a name, a value, and a list of rules; a rule is just `func(T) error`, so any custom validator is a first-class rule with no wrapper:
func (in CreateInput) Validate() valid.Problems {
return valid.Validate(
valid.String("name", in.Name, valid.Required, valid.MinLength(3)),
valid.String("host", in.Host, valid.Required, isSubdomain), // isSubdomain: func(string) error
valid.Number("ttl", in.TTL, valid.Required, valid.Max(100)),
valid.Slice("values", in.Values, valid.NotEmpty, valid.Each(isIPv4)),
)
}
Type safety comes from the per-type constructors: String only accepts Rule[~string], Number only Rule[number], Slice only Rule[[]T] — so a string rule cannot be attached to a number field, and Each([]T)'s element rules are keyed to the element type.
Index ¶
- func Domain(s string) error
- func IP(s string) error
- func NotBlank[T ~string](v T) error
- func NotEmpty[T any](v []T) error
- func Required[T comparable](v T) error
- func URL(s string) error
- type Problems
- type Rule
- type Validatable
- func Number[T number](name string, value T, rules ...Rule[T]) Validatable
- func OptionalString[T ~string](name string, value *T, rules ...Rule[T]) Validatable
- func Slice[T any](name string, value []T, rules ...Rule[[]T]) Validatable
- func String[T ~string](name string, value T, rules ...Rule[T]) Validatable
- func Struct(name string, value Validatable) Validatable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Required ¶
func Required[T comparable](v T) error
Required asserts a comparable value is not its zero value (empty string, zero number, zero-value enum). For slices use NotEmpty.
Types ¶
type Problems ¶
Problems maps a json-style field name to its validation problem. nil means no problems. It is a defined type so it reads as a domain type at call sites and leaves room to grow, while still marshaling like its underlying map and being assignable to a plain map[string]string.
func Validate ¶
func Validate(fields ...Validatable) Problems
Validate runs the given fields and merges their problems into a single flat map of field name -> problem. Within a field the first failing rule wins; if two fields produce the same key the first is kept. It returns nil when everything is valid.
type Rule ¶
Rule validates a value of type T, returning an error whose message becomes the field's problem, or nil when the value is ok.
func Each ¶
Each wraps per-element rules into a single slice rule, reporting "element #i: <problem>" on the first failing element.
func In ¶
In asserts the value equals one of the allowed values. It accepts ~string values (e.g. enum value slices via In(TypeValues()...)) and is usable inside Each.
type Validatable ¶ added in v1.26.0
type Validatable interface {
// Validate returns problems keyed relative to this value: a leaf returns
// {name: msg}, a nested struct returns {name.child: msg, ...}; nil when ok.
Validate() Problems
}
Validatable is anything that validates itself, returning a flat map of keyed problems (nil when ok). Both the leaf fields built by String/Number/Slice and domain types (e.g. service Input types exposing Validate() Problems) satisfy it, so a Validatable can be a single field in a Validate(...) call or a nested struct folded in via Struct, which namespaces its keys.
func Number ¶
func Number[T number](name string, value T, rules ...Rule[T]) Validatable
Number validates a numeric field.
func OptionalString ¶
func OptionalString[T ~string](name string, value *T, rules ...Rule[T]) Validatable
OptionalString validates an optional string field addressed by a pointer. When the pointer is nil the field is treated as absent (no rules run).
func Slice ¶
func Slice[T any](name string, value []T, rules ...Rule[[]T]) Validatable
Slice validates a slice field. Rules operate on the whole slice (e.g. NotEmpty, MinItems); use Each to apply per-element rules.
func String ¶
func String[T ~string](name string, value T, rules ...Rule[T]) Validatable
String validates a string (or ~string, e.g. an enum) field.
func Struct ¶ added in v1.26.0
func Struct(name string, value Validatable) Validatable
Struct validates a nested Validatable, namespacing its problems under name (name.<child key>), so nesting composes to arbitrary depth (a.b.c). Pass a value whose Validate() satisfies Validatable — e.g. &in.Inner when Validate has a pointer receiver. A nil value is skipped, so an optional nested struct can be passed directly. Note: a typed nil pointer is not a nil interface — a caller with an optional *Inner field should pass a genuine nil.