validation

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrValidationFailed = apperr.New("validation_failed")

Functions

func Check

func Check(definition Of) error

Checks a map of label / errors and returns a validation error which contains every errors as needed.

func If

func If(expr bool, fn func() error) error

Simple function to returns the validation result only if the given expr is true.

func Is

func Is[T any](value T, validators ...Validator[T]) error

In many cases, this will be sufficient to apply many validators to one value.

func Maybe

func Maybe[T any](m monad.Maybe[T], fn func(T) error) error

Same as If but executes the fn if the monad has a value.

func NewError

func NewError(fieldErrs map[string]error) error

Builds a new validation error with given invalid fields

func Patch

func Patch[T any](p monad.Patch[T], fn func(T) error) error

Same as Maybe but for monad.Patch. Executes the function only if the value is set and not nil. Looks like I can't pass by an interface to share the logic between Maybe and Patch since Go can't infer the T. I prefer to make the usage easier by duplicating the logic in this specific case. FIXME: When go can infer correctly from an interface [T], merge Maybe and Patch.

func Value

func Value[TRaw, TTarget any](value TRaw, target *TTarget, factory func(TRaw) (TTarget, error)) error

Validate object values by calling their factory and writing to the target in the same call. It makes it easy to validates and instantiates with one call.

func WrapIfAppErr

func WrapIfAppErr(err error, field string, additionalFields ...string) error

Wraps the given error in a new validation error for the specified fields only if it is an app level error. If an infrastructure error is given, it will return immediately without touching it.

Types

type Error

type Error struct {
	Fields map[string]error `json:"fields"`
}

Validation errors struct containing field related errors

func (Error) Error

func (e Error) Error() string

type Of

type Of map[string]error // Tiny shorthand to define a map of validators

type Validator

type Validator[T any] func(T) error // Represents a validator for a specific type

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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