Documentation ¶
Overview ¶
Package validation implements a functional API for consistent struct level validation.
Index ¶
- func HasErrorCode(err error, code ErrorCode) bool
- func JoinErrors[T error](b *strings.Builder, errs []T, indent string)
- type ErrorCode
- type Predicate
- type PropertyError
- type PropertyGetter
- type PropertyRules
- func (r PropertyRules[T, S]) Include(rules ...Validator[T]) 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) []error
- func (r PropertyRules[T, S]) When(predicates ...Predicate[S]) PropertyRules[T, S]
- func (r PropertyRules[T, S]) WithName(name string) PropertyRules[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]
- func Required[T any]() Rule[T]
- type RuleError
- type RuleSet
- type Rules
- type SingleRule
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HasErrorCode ¶ added in v0.50.0
Types ¶
type ErrorCode ¶ added in v0.50.0
type ErrorCode = string
const ( ErrorCodeRequired ErrorCode = "required" 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" ErrorCodeStringLength ErrorCode = "string_length" ErrorCodeStringDescription ErrorCode = "string_description" ErrorCodeStringIsDNSSubdomain ErrorCode = "string_is_dns_subdomain" )
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)
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 RulesFor ¶ added in v0.50.0
func RulesFor[T, S any](getter PropertyGetter[T, S]) PropertyRules[T, S]
RulesFor creates a typed PropertyRules instance for the property which access is defined through getter function.
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]) 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) []error
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 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 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]) WithErrorCode ¶ added in v0.50.0
type SingleRule ¶
type SingleRule[T any] struct { // contains filtered or unexported fields }
func NewSingleRule ¶ added in v0.50.0
func NewSingleRule[T any](validate func(v T) error) SingleRule[T]
func StringDescription ¶
func StringDescription() SingleRule[string]
func StringLength ¶
func StringLength(min, max int) SingleRule[string]
func (SingleRule[T]) Validate ¶
func (r SingleRule[T]) Validate(v T) error
func (SingleRule[T]) WithErrorCode ¶ added in v0.50.0
func (r SingleRule[T]) WithErrorCode(code ErrorCode) SingleRule[T]
Click to show internal directories.
Click to hide internal directories.