validation

package
v0.52.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2023 License: MPL-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package validation implements a functional API for consistent struct level validation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasErrorCode added in v0.50.0

func HasErrorCode(err error, code ErrorCode) bool

func JoinErrors

func JoinErrors[T error](b *strings.Builder, errs []T, indent string)

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 Predicate added in v0.50.0

type Predicate[S any] func(S) bool

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

type Rule[T any] interface {
	Validate(v T) error
}

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]

func Required added in v0.50.0

func Required[T any]() Rule[T]

type RuleError added in v0.50.0

type RuleError struct {
	Message string    `json:"error"`
	Code    ErrorCode `json:"code,omitempty"`
}

func (RuleError) AddCode added in v0.50.0

func (r RuleError) AddCode(code ErrorCode) RuleError

func (RuleError) Error added in v0.50.0

func (r RuleError) Error() string

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 NewRuleSet[T any](rules ...Rule[T]) RuleSet[T]

func StringIsDNSSubdomain

func StringIsDNSSubdomain() RuleSet[string]

func (RuleSet[T]) Validate added in v0.50.0

func (r RuleSet[T]) Validate(v T) error

func (RuleSet[T]) WithErrorCode added in v0.50.0

func (r RuleSet[T]) WithErrorCode(code ErrorCode) RuleSet[T]

type Rules added in v0.50.0

type Rules[S any] interface {
	Validate(v S) []error
}

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]

type Validator added in v0.50.0

type Validator[S any] struct {
	// contains filtered or unexported fields
}

func New added in v0.50.0

func New[S any](rules ...Rules[S]) Validator[S]

func (Validator[S]) Validate added in v0.50.0

func (v Validator[S]) Validate(st S) []error

Jump to

Keyboard shortcuts

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