Documentation ¶
Overview ¶
Package validation implements a functional API for consistent struct level validation.
Index ¶
- Constants
- func HasErrorCode(err error, code ErrorCode) bool
- func JoinErrors[T error](b *strings.Builder, errs []T, indent string)
- func SliceElementName(sliceName string, index int) string
- type ErrorCode
- type HashFunction
- type Predicate
- type PropertyError
- type PropertyErrors
- type PropertyGetter
- type PropertyRules
- func (r PropertyRules[T, S]) Include(rules ...Validator[T]) PropertyRules[T, S]
- func (r PropertyRules[T, S]) Omitempty() PropertyRules[T, S]
- func (r PropertyRules[T, S]) Required() PropertyRules[T, S]
- func (r PropertyRules[T, S]) Rules(rules ...Rule[T]) PropertyRules[T, S]
- func (r PropertyRules[T, S]) StopOnError() PropertyRules[T, S]
- func (r PropertyRules[T, S]) Validate(st S) PropertyErrors
- func (r PropertyRules[T, S]) When(predicates ...Predicate[S]) PropertyRules[T, S]
- func (r PropertyRules[T, S]) WithName(name string) PropertyRules[T, S]
- type PropertyRulesForEach
- func (r PropertyRulesForEach[T, S]) IncludeForEach(rules ...Validator[T]) PropertyRulesForEach[T, S]
- func (r PropertyRulesForEach[T, S]) Rules(rules ...Rule[[]T]) PropertyRulesForEach[T, S]
- func (r PropertyRulesForEach[T, S]) RulesForEach(rules ...Rule[T]) PropertyRulesForEach[T, S]
- func (r PropertyRulesForEach[T, S]) StopOnError() PropertyRulesForEach[T, S]
- func (r PropertyRulesForEach[T, S]) Validate(st S) PropertyErrors
- func (r PropertyRulesForEach[T, S]) When(predicate Predicate[S]) PropertyRulesForEach[T, S]
- func (r PropertyRulesForEach[T, S]) WithName(name string) PropertyRulesForEach[T, S]
- type Rule
- func EqualTo[T comparable](compared T) Rule[T]
- func GreaterThan[T constraints.Ordered](n T) Rule[T]
- func GreaterThanOrEqualTo[T constraints.Ordered](n T) Rule[T]
- func LessThan[T constraints.Ordered](n T) Rule[T]
- func LessThanOrEqualTo[T constraints.Ordered](n T) Rule[T]
- func NotEqualTo[T comparable](compared T) Rule[T]
- type RuleError
- type RuleSet
- type SingleRule
- func Forbidden[T any]() SingleRule[T]
- func MapLength[M ~map[K]V, K comparable, V any](min, max int) SingleRule[M]
- func MapMaxLength[M ~map[K]V, K comparable, V any](max int) SingleRule[M]
- func MapMinLength[M ~map[K]V, K comparable, V any](min int) SingleRule[M]
- func MutuallyExclusive[S any](required bool, getters map[string]func(s S) any) SingleRule[S]
- func NewSingleRule[T any](validate func(v T) error) SingleRule[T]
- func OneOf[T comparable](values ...T) SingleRule[T]
- func Required[T any]() SingleRule[T]
- func SliceLength[S ~[]E, E any](min, max int) SingleRule[S]
- func SliceMaxLength[S ~[]E, E any](max int) SingleRule[S]
- func SliceMinLength[S ~[]E, E any](min int) SingleRule[S]
- func SliceUnique[S []V, V any, H comparable](hashFunc HashFunction[V, H], constraints ...string) SingleRule[S]
- func StringASCII() SingleRule[string]
- func StringContains(substrings ...string) SingleRule[string]
- func StringDenyRegexp(re *regexp.Regexp, examples ...string) SingleRule[string]
- func StringDescription() SingleRule[string]
- func StringJSON() SingleRule[string]
- func StringLength(min, max int) SingleRule[string]
- func StringMatchRegexp(re *regexp.Regexp, examples ...string) SingleRule[string]
- func StringMaxLength(max int) SingleRule[string]
- func StringMinLength(min int) SingleRule[string]
- func StringNotEmpty() SingleRule[string]
- func StringURL() SingleRule[string]
- type Validator
- type ValidatorError
Constants ¶
const ErrorCodeSeparator = ":"
Variables ¶
This section is empty.
Functions ¶
func HasErrorCode ¶ added in v0.50.0
func SliceElementName ¶ added in v0.59.0
Types ¶
type ErrorCode ¶ added in v0.50.0
type ErrorCode = string
const ( ErrorCodeRequired ErrorCode = "required" ErrorCodeForbidden ErrorCode = "forbidden" ErrorCodeEqualTo ErrorCode = "equal_to" ErrorCodeNotEqualTo ErrorCode = "not_equal_to" ErrorCodeGreaterThan ErrorCode = "greater_than" ErrorCodeGreaterThanOrEqualTo ErrorCode = "greater_than_or_equal_to" ErrorCodeLessThan ErrorCode = "less_than" ErrorCodeLessThanOrEqualTo ErrorCode = "less_than_or_equal_to" ErrorCodeStringNotEmpty ErrorCode = "string_not_empty" ErrorCodeStringMatchRegexp ErrorCode = "string_match_regexp" ErrorCodeStringDenyRegexp ErrorCode = "string_deny_regexp" ErrorCodeStringDescription ErrorCode = "string_description" ErrorCodeStringIsDNSSubdomain ErrorCode = "string_is_dns_subdomain" ErrorCodeStringASCII ErrorCode = "string_ascii" ErrorCodeStringURL ErrorCode = "string_url" ErrorCodeStringJSON ErrorCode = "string_json" ErrorCodeStringContains ErrorCode = "string_contains" ErrorCodeStringLength ErrorCode = "string_length" ErrorCodeStringMinLength ErrorCode = "string_min_length" ErrorCodeStringMaxLength ErrorCode = "string_max_length" ErrorCodeSliceLength ErrorCode = "slice_length" ErrorCodeSliceMinLength ErrorCode = "slice_min_length" ErrorCodeSliceMaxLength ErrorCode = "slice_max_length" ErrorCodeMapLength ErrorCode = "map_length" ErrorCodeMapMinLength ErrorCode = "map_min_length" ErrorCodeMapMaxLength ErrorCode = "map_max_length" ErrorCodeOneOf ErrorCode = "one_of" ErrorCodeMutuallyExclusive ErrorCode = "mutually_exclusive" ErrorCodeSliceUnique ErrorCode = "slice_unique" )
type HashFunction ¶ added in v0.59.0
type HashFunction[V any, H comparable] func(v V) H
HashFunction accepts a value and returns a comparable hash.
func SelfHashFunc ¶ added in v0.59.0
func SelfHashFunc[H comparable]() HashFunction[H, H]
SelfHashFunc returns a HashFunction which returns it's input value as a hash itself. The value must be comparable.
type PropertyError ¶ added in v0.50.0
type PropertyError struct { PropertyName string `json:"propertyName"` PropertyValue string `json:"propertyValue"` Errors []*RuleError `json:"errors"` }
func NewPropertyError ¶ added in v0.50.0
func NewPropertyError(propertyName string, propertyValue interface{}, errs ...error) *PropertyError
func (*PropertyError) Error ¶ added in v0.50.0
func (e *PropertyError) Error() string
func (*PropertyError) PrependPropertyName ¶ added in v0.50.0
func (e *PropertyError) PrependPropertyName(name string) *PropertyError
type PropertyErrors ¶ added in v0.59.0
type PropertyErrors []*PropertyError
func (PropertyErrors) Error ¶ added in v0.59.0
func (e PropertyErrors) Error() string
type PropertyGetter ¶ added in v0.50.0
type PropertyGetter[T, S any] func(S) T
func GetSelf ¶ added in v0.50.0
func GetSelf[S any]() PropertyGetter[S, S]
GetSelf is a convenience method for extracting 'self' property of a validated value.
type PropertyRules ¶ added in v0.50.0
type PropertyRules[T, S any] struct { // contains filtered or unexported fields }
PropertyRules is responsible for validating a single property.
func For ¶ added in v0.59.0
func For[T, S any](getter PropertyGetter[T, S]) PropertyRules[T, S]
For creates a typed PropertyRules instance for the property which access is defined through getter function.
func ForPointer ¶ added in v0.59.0
func ForPointer[T, S any](getter PropertyGetter[*T, S]) PropertyRules[T, S]
ForPointer accepts a getter function returning a pointer and wraps its call in order to safely extract the value under the pointer or return a zero value for a give type T. If required is set to true, the nil pointer value will result in an error and the validation will not proceed.
func (PropertyRules[T, S]) Include ¶ added in v0.50.0
func (r PropertyRules[T, S]) Include(rules ...Validator[T]) PropertyRules[T, S]
func (PropertyRules[T, S]) Omitempty ¶ added in v0.59.0
func (r PropertyRules[T, S]) Omitempty() PropertyRules[T, S]
func (PropertyRules[T, S]) Required ¶ added in v0.59.0
func (r PropertyRules[T, S]) Required() PropertyRules[T, S]
func (PropertyRules[T, S]) Rules ¶ added in v0.50.0
func (r PropertyRules[T, S]) Rules(rules ...Rule[T]) PropertyRules[T, S]
func (PropertyRules[T, S]) StopOnError ¶ added in v0.50.0
func (r PropertyRules[T, S]) StopOnError() PropertyRules[T, S]
func (PropertyRules[T, S]) Validate ¶ added in v0.50.0
func (r PropertyRules[T, S]) Validate(st S) PropertyErrors
Validate validates the property value using provided rules. nolint: gocognit
func (PropertyRules[T, S]) When ¶ added in v0.50.0
func (r PropertyRules[T, S]) When(predicates ...Predicate[S]) PropertyRules[T, S]
func (PropertyRules[T, S]) WithName ¶ added in v0.50.0
func (r PropertyRules[T, S]) WithName(name string) PropertyRules[T, S]
type PropertyRulesForEach ¶ added in v0.59.0
type PropertyRulesForEach[T, S any] struct { // contains filtered or unexported fields }
PropertyRulesForEach is responsible for validating a single property.
func ForEach ¶ added in v0.59.0
func ForEach[T, S any](getter PropertyGetter[[]T, S]) PropertyRulesForEach[T, S]
ForEach creates a typed PropertyRules instance for a slice property which access is defined through getter function.
func (PropertyRulesForEach[T, S]) IncludeForEach ¶ added in v0.59.0
func (r PropertyRulesForEach[T, S]) IncludeForEach(rules ...Validator[T]) PropertyRulesForEach[T, S]
func (PropertyRulesForEach[T, S]) Rules ¶ added in v0.59.0
func (r PropertyRulesForEach[T, S]) Rules(rules ...Rule[[]T]) PropertyRulesForEach[T, S]
func (PropertyRulesForEach[T, S]) RulesForEach ¶ added in v0.59.0
func (r PropertyRulesForEach[T, S]) RulesForEach(rules ...Rule[T]) PropertyRulesForEach[T, S]
func (PropertyRulesForEach[T, S]) StopOnError ¶ added in v0.59.0
func (r PropertyRulesForEach[T, S]) StopOnError() PropertyRulesForEach[T, S]
func (PropertyRulesForEach[T, S]) Validate ¶ added in v0.59.0
func (r PropertyRulesForEach[T, S]) Validate(st S) PropertyErrors
Validate executes each of the rules sequentially and aggregates the encountered errors. nolint: prealloc, gocognit
func (PropertyRulesForEach[T, S]) When ¶ added in v0.59.0
func (r PropertyRulesForEach[T, S]) When(predicate Predicate[S]) PropertyRulesForEach[T, S]
func (PropertyRulesForEach[T, S]) WithName ¶ added in v0.59.0
func (r PropertyRulesForEach[T, S]) WithName(name string) PropertyRulesForEach[T, S]
type Rule ¶
Rule is the interface for all validation rules.
func EqualTo ¶ added in v0.50.0
func EqualTo[T comparable](compared T) Rule[T]
func GreaterThan ¶ added in v0.50.0
func GreaterThan[T constraints.Ordered](n T) Rule[T]
func GreaterThanOrEqualTo ¶ added in v0.50.0
func GreaterThanOrEqualTo[T constraints.Ordered](n T) Rule[T]
func LessThan ¶ added in v0.50.0
func LessThan[T constraints.Ordered](n T) Rule[T]
func LessThanOrEqualTo ¶ added in v0.50.0
func LessThanOrEqualTo[T constraints.Ordered](n T) Rule[T]
func NotEqualTo ¶ added in v0.50.0
func NotEqualTo[T comparable](compared T) Rule[T]
type RuleError ¶ added in v0.50.0
func NewRequiredError ¶ added in v0.59.0
func NewRequiredError() *RuleError
type RuleSet ¶ added in v0.50.0
type RuleSet[T any] struct { // contains filtered or unexported fields }
RuleSet allows defining Rule which aggregates multiple sub-rules.
func NewRuleSet ¶ added in v0.50.0
func StringIsDNSSubdomain ¶
func (RuleSet[T]) WithDetails ¶ added in v0.59.0
func (RuleSet[T]) WithErrorCode ¶ added in v0.50.0
type SingleRule ¶
type SingleRule[T any] struct { // contains filtered or unexported fields }
func Forbidden ¶ added in v0.59.0
func Forbidden[T any]() SingleRule[T]
func MapLength ¶ added in v0.59.0
func MapLength[M ~map[K]V, K comparable, V any](min, max int) SingleRule[M]
func MapMaxLength ¶ added in v0.59.0
func MapMaxLength[M ~map[K]V, K comparable, V any](max int) SingleRule[M]
func MapMinLength ¶ added in v0.59.0
func MapMinLength[M ~map[K]V, K comparable, V any](min int) SingleRule[M]
func MutuallyExclusive ¶ added in v0.61.0
func MutuallyExclusive[S any](required bool, getters map[string]func(s S) any) SingleRule[S]
MutuallyExclusive checks if properties are mutually exclusive. This means, exactly one of the properties can be provided. If required is true, then a single non-empty property is required.
func NewSingleRule ¶ added in v0.50.0
func NewSingleRule[T any](validate func(v T) error) SingleRule[T]
func OneOf ¶ added in v0.59.0
func OneOf[T comparable](values ...T) SingleRule[T]
func Required ¶ added in v0.50.0
func Required[T any]() SingleRule[T]
func SliceLength ¶ added in v0.59.0
func SliceLength[S ~[]E, E any](min, max int) SingleRule[S]
func SliceMaxLength ¶ added in v0.59.0
func SliceMaxLength[S ~[]E, E any](max int) SingleRule[S]
func SliceMinLength ¶ added in v0.59.0
func SliceMinLength[S ~[]E, E any](min int) SingleRule[S]
func SliceUnique ¶ added in v0.59.0
func SliceUnique[S []V, V any, H comparable](hashFunc HashFunction[V, H], constraints ...string) SingleRule[S]
SliceUnique validates that a slice contains unique elements based on a provided HashFunction. You can optionally specify constraints which will be included in the error message to further clarify the reason for breaking uniqueness.
func StringASCII ¶ added in v0.59.0
func StringASCII() SingleRule[string]
func StringContains ¶ added in v0.59.0
func StringContains(substrings ...string) SingleRule[string]
func StringDenyRegexp ¶ added in v0.59.0
func StringDenyRegexp(re *regexp.Regexp, examples ...string) SingleRule[string]
func StringDescription ¶
func StringDescription() SingleRule[string]
func StringJSON ¶ added in v0.59.0
func StringJSON() SingleRule[string]
func StringLength ¶
func StringLength(min, max int) SingleRule[string]
func StringMatchRegexp ¶ added in v0.59.0
func StringMatchRegexp(re *regexp.Regexp, examples ...string) SingleRule[string]
func StringMaxLength ¶ added in v0.59.0
func StringMaxLength(max int) SingleRule[string]
func StringMinLength ¶ added in v0.59.0
func StringMinLength(min int) SingleRule[string]
func StringNotEmpty ¶ added in v0.59.0
func StringNotEmpty() SingleRule[string]
func StringURL ¶ added in v0.59.0
func StringURL() SingleRule[string]
func (SingleRule[T]) Validate ¶
func (r SingleRule[T]) Validate(v T) error
func (SingleRule[T]) WithDetails ¶ added in v0.59.0
func (r SingleRule[T]) WithDetails(format string, a ...any) SingleRule[T]
func (SingleRule[T]) WithErrorCode ¶ added in v0.50.0
func (r SingleRule[T]) WithErrorCode(code ErrorCode) SingleRule[T]
type Validator ¶ added in v0.50.0
type Validator[S any] struct { // contains filtered or unexported fields }
func (Validator[S]) Validate ¶ added in v0.50.0
func (v Validator[S]) Validate(st S) *ValidatorError
type ValidatorError ¶ added in v0.59.0
type ValidatorError struct { Errors PropertyErrors `json:"errors"` Name string `json:"name"` }
func NewValidatorError ¶ added in v0.59.0
func NewValidatorError(errs PropertyErrors) *ValidatorError
func (*ValidatorError) Error ¶ added in v0.59.0
func (e *ValidatorError) Error() string
func (*ValidatorError) WithName ¶ added in v0.59.0
func (e *ValidatorError) WithName(name string) *ValidatorError