validate

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must[Value any](t *testing.T, value Value, validators ...Validator[Value])

Must validate the given value using the provided validators and calls t.Error if any validation errors occur. It collects all errors from all validators.

Types

type BasicTypes

type BasicTypes interface {
	~string | ~bool | NumericTypes
}

BasicTypes represents types that are basic types

type Errors

type Errors []string

func (Errors) Error

func (errs Errors) Error() string

func (Errors) IsValid

func (errs Errors) IsValid() bool

type FieldDescriptor

type FieldDescriptor[Struct any, Field any] interface {
	GetName() string
	GetValue(*Struct) Field
}

type FuncValidator

type FuncValidator[T any] func(T) Errors

func (FuncValidator[T]) Validate

func (f FuncValidator[T]) Validate(v T) Errors

type NumericTypes

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

NumericTypes represents types that support comparison operators

type Validator

type Validator[T any] interface {
	Validate(T) Errors
}

func DeepEqual added in v1.0.1

func DeepEqual[Value any](expected Value) Validator[Value]

DeepEqual returns a validator that checks if the actual value is deeply equal to the expected value using reflect.DeepEqual.

func Field

func Field[Struct any, Field any](descriptor FieldDescriptor[Struct, Field], validators ...Validator[Field]) Validator[*Struct]

Field returns a validator for a struct field described by `descriptor`. The returned validator extracts the field value from the parent struct and runs the provided validators; any errors are prefixed with the field name.

func Max

func Max[Value NumericTypes](max Value) Validator[Value]

Max creates a condition that validates a numeric value is less than or equal to the maximum.

func MaxCount

func MaxCount[Element any](maxLength int) Validator[[]Element]

MaxCount returns a validator that ensures a slice has at most `maxLength` elements.

func MaxRunes

func MaxRunes(max int) Validator[string]

MaxRunes returns a validator that ensures the string contains at most `max` runes (Unicode code points).

func Min

func Min[Value NumericTypes](min Value) Validator[Value]

Min creates a condition that validates a numeric value is greater than or equal to the minimum.

func MinCount

func MinCount[Element any](minLength int) Validator[[]Element]

MinCount returns a validator that ensures a slice has at least `minLength` elements.

func MinRunes

func MinRunes(min int) Validator[string]

MinRunes returns a validator that ensures the string contains at least `min` runes (Unicode code points).

func OneOf

func OneOf[Element comparable](values ...Element) Validator[Element]

OneOf returns a `Validator` that checks whether a value is one of the provided `values`.

func Regex

func Regex(regex *regexp.Regexp) Validator[string]

Regex returns a validator that checks the given string matches the provided regular expression.

It panics if `regex` is nil.

func RunesExactly

func RunesExactly(length int) Validator[string]

RunesExactly returns a validator that ensures the string contains exactly `length` runes (Unicode code points).

func Slice

func Slice[Element any](validators ...Validator[Element]) Validator[[]Element]

Slice returns a validator that applies the provided element validators to each element of a slice. If any element produces validation errors the returned result will contain those errors prefixed with the element index.

func Struct

func Struct[Struct any](validators ...Validator[*Struct]) Validator[*Struct]

Struct returns a validator for pointer-to-struct values that runs the provided field validators and aggregates their errors.

func When

func When[T any](condition func(T) bool, validators ...Validator[T]) Validator[T]

When returns a conditional validator that runs the provided validators only when `condition` evaluates to true for the validated value.

func WhenNotEmpty added in v1.0.4

func WhenNotEmpty(validators ...Validator[string]) Validator[string]

WhenNotEmpty returns a validator for string that runs the provided validators only when the string is not empty

func WhenNotNil

func WhenNotNil[Struct any](validators ...Validator[*Struct]) Validator[*Struct]

WhenNotNil returns a validator for pointer-to-struct values that runs the provided validators only when the pointer is non-nil. This is a convenience wrapper around `When` that checks `v != nil` before executing the nested validators, preventing nil dereferences in validators that assume a non-nil receiver.

Jump to

Keyboard shortcuts

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