valid

package
v1.25.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 6 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func NotBlank

func NotBlank[T ~string](v T) error

NotBlank asserts the string is not empty after trimming whitespace.

func NotEmpty

func NotEmpty[T any](v []T) error

NotEmpty asserts the slice has at least one element.

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 Field

type Field interface {
	Validate() (name string, err error)
}

Field is a single validated struct field. Struct collects the problem reported by each field.

func Number

func Number[T number](name string, value T, rules ...Rule[T]) Field

Number validates a numeric field.

func OptionalString

func OptionalString[T ~string](name string, value *T, rules ...Rule[T]) Field

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]) Field

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]) Field

String validates a string (or ~string, e.g. an enum) field.

type Problems

type Problems map[string]string

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 ...Field) Problems

Validate runs the given fields and returns a map of field name -> problem. Within a field the first failing rule wins; if two fields share a name the first is kept. It returns nil when everything is valid.

func (Problems) Ok

func (p Problems) Ok() bool

Ok reports whether there are no problems.

type Rule

type Rule[T any] func(T) error

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

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

Each wraps per-element rules into a single slice rule, reporting "element #i: <problem>" on the first failing element.

func In

func In[T ~string](allowed ...T) Rule[T]

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.

func Match

func Match(re *regexp.Regexp, msg string) Rule[string]

Match asserts the string matches re, reporting msg on failure.

func Max

func Max[T number](n T) Rule[T]

Max asserts the number is at most n.

func MaxLength

func MaxLength(n int) Rule[string]

MaxLength asserts the string has at most n characters.

func Min

func Min[T number](n T) Rule[T]

Min asserts the number is at least n.

func MinLength

func MinLength(n int) Rule[string]

MinLength asserts the string has at least n characters.

Jump to

Keyboard shortcuts

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