Documentation ¶
Index ¶
- func FormatRange[T NumberType](min, max T) string
- func Fullname(s string) string
- func InRange[T NumberType](min, max T) func(T) string
- func Join(errs ...errors.ValidationError) error
- func Negative[T NumberType](n T) string
- func NonZero[T NumberType](n T) string
- func NotEmpty(s string) string
- func NotEmptySlice[T any, V SliceType[T]](s V) string
- func Positive[T NumberType](n T) string
- func SliceLength[T any, V SliceType[T]](min, max int) func(s V) string
- func StringLength(min, max int) func(s string) string
- func Zero[T NumberType](n T) string
- type FieldError
- type NumberError
- type NumberType
- type SliceError
- type SliceType
- type StringError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatRange ¶
func FormatRange[T NumberType](min, max T) string
FormatRange returns a string representation of a range of values, such as "[1, 10]". It uses a type constraint NumberValue, which means that the parameters must be numeric types that support comparison and formatting operations.
func InRange ¶
func InRange[T NumberType](min, max T) func(T) string
InRange accepts any numeric type and returns a message if the value is not in the range.
func Join ¶
func Join(errs ...errors.ValidationError) error
Join joins the given validation errors into one.
func Negative ¶
func Negative[T NumberType](n T) string
Negative accepts any numeric type and returns a message if the value is not negative.
func NonZero ¶
func NonZero[T NumberType](n T) string
NonZero accepts any numeric type and returns a message if the value is not zero.
func NotEmptySlice ¶
NotEmptySlice accepts any slice and returns a message if the value is empty.
func Positive ¶
func Positive[T NumberType](n T) string
Positive accepts any numeric type and returns a message if the value is not positive.
func SliceLength ¶
SliceLength accepts any slice and returns a message if the length is not in the given range.
func StringLength ¶
StringLength accepts any string and returns a message if the length is not in the given range.
func Zero ¶
func Zero[T NumberType](n T) string
Zero accepts any numeric type and returns a message if the value is not zero.
Types ¶
type FieldError ¶
type FieldError[T any] struct { Field string `json:"field"` Value T `json:"value"` Reason string `json:"reason"` }
FieldError describes a field validation error. It completes the errors.ValidationError interface.
func Field ¶
func Field[T any](field string, value T) *FieldError[T]
Field returns a new validation error.
Use its Func method to add validations over this field.
func (*FieldError[T]) Error ¶
func (e *FieldError[T]) Error() string
Error completes the standard error interface.
func (*FieldError[T]) Func ¶
func (e *FieldError[T]) Func(fns ...func(value T) string) *FieldError[T]
Func accepts a variadic number of functions which accept the value of the field and return a string message if the value is invalid. It joins the reasons into one.
func (*FieldError[T]) GetField ¶
func (e *FieldError[T]) GetField() string
GetField returns the field name.
func (*FieldError[T]) GetReason ¶
func (e *FieldError[T]) GetReason() string
GetReason returns the reason of the validation error.
func (*FieldError[T]) GetValue ¶
func (e *FieldError[T]) GetValue() any
GetValue returns the value of the field.
func (*FieldError[T]) IsZero ¶
func (e *FieldError[T]) IsZero() bool
IsZero reports whether the error is nil or has an empty reason.
type NumberError ¶
type NumberError[T NumberType] struct{ *FieldError[T] }
NumberError describes a number field validation error.
func Number ¶
func Number[T NumberType](field string, value T) *NumberError[T]
Number returns a new number validation error.
func (*NumberError[T]) InRange ¶
func (e *NumberError[T]) InRange(min, max T) *NumberError[T]
InRange adds an error if the value is not in the range.
func (*NumberError[T]) Negative ¶
func (e *NumberError[T]) Negative() *NumberError[T]
Negative adds an error if the value is not negative.
func (*NumberError[T]) NonZero ¶
func (e *NumberError[T]) NonZero() *NumberError[T]
NonZero adds an error if the value is zero.
func (*NumberError[T]) Positive ¶
func (e *NumberError[T]) Positive() *NumberError[T]
Positive adds an error if the value is not positive.
func (*NumberError[T]) Zero ¶
func (e *NumberError[T]) Zero() *NumberError[T]
Zero reports whether the value is zero.
type NumberType ¶
type NumberType interface { constraints.Integer | constraints.Float }
NumberType is a type constraint that accepts any numeric type.
type SliceError ¶
type SliceError[T any, V SliceType[T]] struct{ *FieldError[V] }
SliceError describes a slice field validation error.
func Slice ¶
func Slice[T any, V SliceType[T]](field string, value V) *SliceError[T, V]
Slice returns a new slice validation error.
func (*SliceError[T, V]) Length ¶
func (e *SliceError[T, V]) Length(min, max int) *SliceError[T, V]
Length adds an error if the slice length is not in the given range.
func (*SliceError[T, V]) NotEmpty ¶
func (e *SliceError[T, V]) NotEmpty() *SliceError[T, V]
NotEmpty adds an error if the slice is empty.
type SliceType ¶
type SliceType[T any] interface { ~[]T }
SliceType is a type constraint that accepts any slice type.
type StringError ¶
type StringError struct{ *FieldError[string] }
StringError describes a string field validation error.
func String ¶
func String(field string, value string) *StringError
String returns a new string validation error.
func (*StringError) Fullname ¶
func (e *StringError) Fullname() *StringError
Fullname adds an error if the string is not a full name.
func (*StringError) Length ¶
func (e *StringError) Length(min, max int) *StringError
Length adds an error if the string length is not in the given range.
func (*StringError) NotEmpty ¶
func (e *StringError) NotEmpty() *StringError
NotEmpty adds an error if the string is empty.