rule

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After(t time.Time) types.Rule[time.Time]

After validates that the time is after t.

func And

func And[T any](rules ...types.Rule[T]) types.Rule[T]

And combines multiple rules into one, returning the first error encountered.

func Before

func Before(t time.Time) types.Rule[time.Time]

Before validates that the time is before t.

func Between

func Between(min, max time.Time) types.Rule[time.Time]

Between validates that the time is between min and max inclusive.

func ContainsAnyChar

func ContainsAnyChar[T ~string](chars string) types.Rule[T]

ContainsAnyChar validates that the string contains any Unicode code point in chars.

func ContainsElem

func ContainsElem[S ~[]T, T comparable](s *S, v T) types.Rule[S]

ContainsElem validates that the slice contains v.

func ContainsRune

func ContainsRune[T ~string](r rune) types.Rule[T]

ContainsRune validates that the string contains the given rune.

func ContainsString

func ContainsString[T ~string](substr string) types.Rule[T]

ContainsString validates that the string contains substr.

func Deref

func Deref[T any](rules ...types.Rule[*T]) types.Rule[T]

Deref lifts pointer rules into a value rule by taking the address of the value.

func Equal

func Equal[T comparable](eq T) types.Rule[T]

Equal validates that the value equals eq.

func EqualIgnoreCase

func EqualIgnoreCase[T ~string](eq string) types.Rule[T]

EqualIgnoreCase validates that the string equals eq, ignoring case.

func EqualMap

func EqualMap[M ~map[K]T, K, T comparable](m M) types.Rule[M]

EqualMap validates that the map is equal to m using comparable value equality.

func EqualMapFunc

func EqualMapFunc[M1 ~map[K]T1, M2 ~map[K]T2, K comparable, T1, T2 any](m *M1, m2 M2, eq func(T1, T2) bool) types.Rule[M1]

EqualMapFunc validates that the map is equal to m2 using a custom equality function. m is only passed to help the compiler with type inference.

func EqualSlice

func EqualSlice[S ~[]T, T comparable](s2 S) types.Rule[S]

EqualSlice validates that the slice is equal to s2.

func EqualSliceFunc

func EqualSliceFunc[S1 ~[]T1, S2 ~[]T2, T1, T2 any](s *S1, s2 S2, eq func(T1, T2) bool) types.Rule[S1]

EqualSliceFunc validates that the slice is equal to s2 using a custom equality function. s is only passed to help the compiler with type inference.

func EqualToAny

func EqualToAny[T comparable](eq ...T) types.Rule[T]

EqualToAny validates that the value equals at least one of the provided values.

func ForEach

func ForEach[S ~[]T, T any](s *S, rule types.Rule[T]) types.Rule[S]

ForEach validates each slice element against the provided rule.

func ForEachKey

func ForEachKey[M ~map[K]T, K comparable, T any](m *M, rule types.Rule[K]) types.Rule[M]

ForEachKey validates each map key against the provided rule.

func ForEachValue

func ForEachValue[M ~map[K]T, K comparable, T any](m *M, rule types.Rule[T]) types.Rule[M]

ForEachValue validates each map value against the provided rule.

func ForKey

func ForKey[M ~map[K]T, K comparable, T any](m *M, key K, rule types.Rule[T]) types.Rule[M]

ForKey validates the value at a specific map key against the provided rule. Returns an error if the key is not present in the map.

func GreaterThan

func GreaterThan[T cmp.Ordered](ge T) types.Rule[T]

GreaterThan validates that the value is strictly greater than ge.

func GreaterThanEqual

func GreaterThanEqual[T cmp.Ordered](ge T) types.Rule[T]

GreaterThanEqual validates that the value is greater than or equal to ge.

func HasPrefix

func HasPrefix[T ~string](prefix string) types.Rule[T]

HasPrefix validates that the string has the given prefix.

func HasSuffix

func HasSuffix[T ~string](suffix string) types.Rule[T]

HasSuffix validates that the string has the given suffix.

func If

func If[T any](condition bool, conditionalRule types.Rule[T]) types.Rule[T]

If returns the rule when condition is true, or nil when false. The nil result is safely skipped by And and the validation layer — do not call it directly.

func InTheFuture

func InTheFuture() types.Rule[time.Time]

InTheFuture validates that the time is after the current time.

func InTheFutureOrPresent

func InTheFutureOrPresent() types.Rule[time.Time]

InTheFutureOrPresent validates that the time is after or equal to the current time.

func InThePast

func InThePast() types.Rule[time.Time]

InThePast validates that the time is before the current time.

func InThePastOrPresent

func InThePastOrPresent() types.Rule[time.Time]

InThePastOrPresent validates that the time is before or equal to the current time.

func IsBlank

func IsBlank[T ~string]() types.Rule[T]

IsBlank validates that the string is empty or whitespace-only.

func IsFalse

func IsFalse[T ~bool]() types.Rule[T]

IsFalse validates that the value is false.

func IsNil

func IsNil[T any]() types.Rule[*T]

IsNil validates that the pointer is nil.

func IsNotNil

func IsNotNil[T any]() types.Rule[*T]

IsNotNil validates that the pointer is not nil.

func IsNotZeroValue

func IsNotZeroValue[T comparable]() types.Rule[T]

IsNotZeroValue validates that the value does not equal its type's zero value.

func IsSorted

func IsSorted[S ~[]T, T cmp.Ordered](s *S) types.Rule[S]

IsSorted validates that the slice is sorted in ascending order. s is only passed to help the compiler with type inference.

func IsSortedFunc

func IsSortedFunc[S ~[]T, T any](s *S, cmpFn func(T, T) int) types.Rule[S]

IsSortedFunc validates that the slice is sorted according to the provided comparison function. s is only passed to help the compiler with type inference.

func IsTrue

func IsTrue[T ~bool]() types.Rule[T]

IsTrue validates that the value is true.

func IsValid

func IsValid[T validation.Validatable]() types.Rule[T]

IsValid validates that the value passes its own Validation() rules.

func IsZeroValue

func IsZeroValue[T comparable]() types.Rule[T]

IsZeroValue validates that the value equals its type's zero value.

func LengthMap

func LengthMap[M ~map[K]T, K comparable, T any](m *M, l int) types.Rule[M]

LengthMap validates that the map has exactly l entries.

func LengthRune

func LengthRune[T ~string](l int) types.Rule[T]

LengthRune validates that the string contains exactly l Unicode code points (runes). Note: a single visible character may span multiple code points. For example, the family emoji 👨‍👩‍👧 is composed of several code points joined by zero-width joiners, so LengthRune counts it as more than 1. If you need to validate display length (e.g. for usernames shown in a UI), use a grapheme-cluster-aware library such as github.com/rivo/uniseg instead.

func LengthSlice

func LengthSlice[S ~[]T, T any](s *S, l int) types.Rule[S]

LengthSlice validates that the slice has exactly l elements.

func LengthString

func LengthString[T ~string](l int) types.Rule[T]

LengthString validates that the string has exactly l bytes. Use LengthRune for character count.

func LessThan

func LessThan[T cmp.Ordered](le T) types.Rule[T]

LessThan validates that the value is strictly less than le.

func LessThanEqual

func LessThanEqual[T cmp.Ordered](le T) types.Rule[T]

LessThanEqual validates that the value is less than or equal to le.

func MatchBytes

func MatchBytes[T ~[]byte](re *regexp.Regexp) types.Rule[T]

MatchBytes validates that the byte slice matches the provided regular expression.

func MatchString

func MatchString[T ~string](re *regexp.Regexp) types.Rule[T]

MatchString validates that the string matches the provided regular expression.

func Max

func Max[T cmp.Ordered](max T) types.Rule[T]

Max validates that the value is less than or equal to max.

func MaxLengthMap

func MaxLengthMap[M ~map[K]T, K comparable, T any](m *M, l int) types.Rule[M]

MaxLengthMap validates that the map has at most l entries.

func MaxLengthRune

func MaxLengthRune[T ~string](l int) types.Rule[T]

MaxLengthRune validates that the string contains at most l Unicode code points.

func MaxLengthSlice

func MaxLengthSlice[S ~[]T, T any](s *S, l int) types.Rule[S]

MaxLengthSlice validates that the slice has at most l elements.

func MaxLengthString

func MaxLengthString[T ~string](l int) types.Rule[T]

MaxLengthString validates that the string has at most l bytes.

func Min

func Min[T cmp.Ordered](min T) types.Rule[T]

Min validates that the value is greater than or equal to min.

func MinLengthMap

func MinLengthMap[M ~map[K]T, K comparable, T any](m *M, l int) types.Rule[M]

MinLengthMap validates that the map has at least l entries.

func MinLengthRune

func MinLengthRune[T ~string](l int) types.Rule[T]

MinLengthRune validates that the string contains at least l Unicode code points.

func MinLengthSlice

func MinLengthSlice[S ~[]T, T any](s *S, l int) types.Rule[S]

MinLengthSlice validates that the slice has at least l elements.

func MinLengthString

func MinLengthString[T ~string](l int) types.Rule[T]

MinLengthString validates that the string has at least l bytes.

func Negative

func Negative[T cmp.Ordered]() types.Rule[T]

Negative validates that the value is less than zero.

func NegativeOrZero

func NegativeOrZero[T cmp.Ordered]() types.Rule[T]

NegativeOrZero validates that the value is less than or equal to zero.

func NotBlank

func NotBlank[T ~string]() types.Rule[T]

NotBlank validates that the string is not empty or whitespace-only.

func NotContainsAnyChar

func NotContainsAnyChar[T ~string](chars string) types.Rule[T]

NotContainsAnyChar validates that the string contains none of the Unicode code points in chars.

func NotContainsElem

func NotContainsElem[S ~[]T, T comparable](s *S, v T) types.Rule[S]

NotContainsElem validates that the slice does not contain v.

func NotContainsRune

func NotContainsRune[T ~string](r rune) types.Rule[T]

NotContainsRune validates that the string does not contain the given rune.

func NotContainsString

func NotContainsString[T ~string](substr string) types.Rule[T]

NotContainsString validates that the string does not contain substr.

func NotEqual

func NotEqual[T comparable](ne T) types.Rule[T]

NotEqual validates that the value does not equal ne.

func NotEqualIgnoreCase

func NotEqualIgnoreCase[T ~string](eq string) types.Rule[T]

NotEqualIgnoreCase validates that the string does not equal eq, ignoring case.

func NotEqualToAny

func NotEqualToAny[T comparable](ne ...T) types.Rule[T]

NotEqualToAny validates that the value does not equal any of the provided values.

func NotHasPrefix

func NotHasPrefix[T ~string](prefix string) types.Rule[T]

NotHasPrefix validates that the string does not have the given prefix.

func NotHasSuffix

func NotHasSuffix[T ~string](suffix string) types.Rule[T]

NotHasSuffix validates that the string does not have the given suffix.

func NotInRange

func NotInRange[T cmp.Ordered](min, max T) types.Rule[T]

NotInRange validates that the value is not between min and max inclusive.

func NotMatchBytes

func NotMatchBytes[T ~[]byte](re *regexp.Regexp) types.Rule[T]

NotMatchBytes validates that the byte slice does not match the provided regular expression.

func NotMatchString

func NotMatchString[T ~string](re *regexp.Regexp) types.Rule[T]

NotMatchString validates that the string does not match the provided regular expression.

func OnDate

func OnDate(t time.Time) types.Rule[time.Time]

OnDate validates that the time falls on the same calendar date as t, using t's location.

func Or

func Or[T any](rules ...types.Rule[T]) types.Rule[T]

Or combines multiple rules into one, returning nil if any rule passes. Returns the last error if all rules fail. Nil rules are skipped.

func Positive

func Positive[T cmp.Ordered]() types.Rule[T]

Positive validates that the value is greater than zero.

func PositiveOrZero

func PositiveOrZero[T cmp.Ordered]() types.Rule[T]

PositiveOrZero validates that the value is greater than or equal to zero.

func Range

func Range[T cmp.Ordered](min, max T) types.Rule[T]

Range validates that the value is between min and max inclusive.

func Ref

func Ref[T any](rules ...types.Rule[T]) types.Rule[*T]

Ref lifts value rules into a pointer rule. Returns an error if the pointer is nil.

func Unique

func Unique[S ~[]T, T comparable](s *S) types.Rule[S]

Unique validates that all elements in the slice are distinct.

Types

This section is empty.

Jump to

Keyboard shortcuts

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