validation

package
v12.2.11 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Index

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 Fullname

func Fullname(s string) string

Fullname accepts any string and returns a message if the value is not a full name.

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 NotEmpty

func NotEmpty(s string) string

NotEmpty accepts any string and returns a message if the value is empty.

func NotEmptySlice

func NotEmptySlice[T any, V SliceType[T]](s V) string

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

func SliceLength[T any, V SliceType[T]](min, max int) func(s V) string

SliceLength accepts any slice and returns a message if the length is not in the given range.

func StringLength

func StringLength(min, max int) func(s string) string

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.

Jump to

Keyboard shortcuts

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