validation

package
v0.0.0-...-a26ebea Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2022 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Check

type Check[T any] func(val T) *Error

Check is a function that performs a validation check of a value of type T.

type Checker

type Checker interface {
	Check() ErrorGroup
}

Checker represents types that can be checked for validation errors.

type Error

type Error struct {
	// Unique numeric value that represents a specific type of check error.
	Code uint32
	// Unique human understandable name of the error.
	Slug string
	// Full description of the error with any additional context.
	Desc string
}

Error represents the failure of a validation check.

func NewError

func NewError(slug, desc string) *Error

NewError creates a new Error for the given slug/desc.

func (*Error) Error

func (e *Error) Error() string

Error string value of the Error struct.

Interface: error

type ErrorGroup

type ErrorGroup interface {
	error

	Append(err ...*Error)
	Errors() []*Error
	NilWhenEmpty() ErrorGroup
	Unwrap() error
}

ErrorGroup represents a grouping of validation errors.

func RunCheckers

func RunCheckers(checkers ...Checker) ErrorGroup

RunCheckers runs checkers and bundles all errors into a group.

func RunChecks

func RunChecks[T any](val T, checks ...Check[T]) ErrorGroup

RunChecks runs checks for the given value.

If the value is invalid, one or more Errors will be returned.

type Errors

type Errors struct {
	// contains filtered or unexported fields
}

Errors represents a slice of Error that implements the ErrorGroup interface.

func (*Errors) Append

func (e *Errors) Append(err ...*Error)

Append adds a new Error to the group.

Interface: ErrorGroup

func (*Errors) Error

func (e *Errors) Error() string

Error string value of the Errors struct.

Interface: error

func (*Errors) Errors

func (e *Errors) Errors() []*Error

Errors returns a slice of errors in the group.

Interface: ErrorGroup

func (*Errors) NilWhenEmpty

func (e *Errors) NilWhenEmpty() ErrorGroup

NilWhenEmpty will return nil if the instance is nil or doesn't contain any errors. This is helpful for callers to return the result of this function after accumulating errors in a loop.

Interface: ErrorGroup

func (*Errors) Unwrap

func (e *Errors) Unwrap() error

Unwrap returns the next error in the slice or nil if there are no more errors.

Interface: errors.Unwrap, ErrorGroup

Jump to

Keyboard shortcuts

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