data

package
v0.0.0-...-5f538b7 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2025 License: 0BSD Imports: 3 Imported by: 0

Documentation

Overview

Package data provides ways to declare validation constraints on values, these constraints can be reflected upon at runtime.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Absent

func Absent[T any](value **T) error

Absent reports an error if the pointer is nil.

func Exists

func Exists[T any](value **T, fn func(*T) error) error

Exists runs the given function on a pointer value only if the value is not nil.

Types

type ErrExceeds

type ErrExceeds struct {
	Limit string
	// contains filtered or unexported fields
}

func (*ErrExceeds) Error

func (err *ErrExceeds) Error() string

func (*ErrExceeds) FieldName

func (err *ErrExceeds) FieldName() string

func (*ErrExceeds) StatusHTTP

func (err *ErrExceeds) StatusHTTP() int

func (*ErrExceeds) WithFieldNameSetTo

func (err *ErrExceeds) WithFieldNameSetTo(name string) error

type ErrInvalid

type ErrInvalid struct {
	Class string
	Hints string
	// contains filtered or unexported fields
}

func (*ErrInvalid) Error

func (err *ErrInvalid) Error() string

func (*ErrInvalid) FieldName

func (err *ErrInvalid) FieldName() string

func (*ErrInvalid) StatusHTTP

func (err *ErrInvalid) StatusHTTP() int

func (*ErrInvalid) WithFieldNameSetTo

func (err *ErrInvalid) WithFieldNameSetTo(name string) error

type ErrMissing

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

func (*ErrMissing) Error

func (err *ErrMissing) Error() string

func (*ErrMissing) FieldName

func (err *ErrMissing) FieldName() string

func (*ErrMissing) StatusHTTP

func (err *ErrMissing) StatusHTTP() int

func (*ErrMissing) WithFieldNameSetTo

func (err *ErrMissing) WithFieldNameSetTo(name string) error

type ReportingMapped

type ReportingMapped[K comparable, V any] struct {
	// Invalid reports an error if the number does not match the given
	// 'format' string. 'hints' are optional and can be used to provide
	// additional information about the specification of the format.
	Invalid func(class string, fn func(map[K]V) bool, hints ...string) error
	// Exceeds reports an error if the length of the map is greater
	// than the given limit.
	Exceeds func(int) error
	// ForEach iterates over each element in the map and reports an
	// error if the given function returns an error.
	ForEach func(func(*V) error) error
	// MapKeys iterates over each key in the map and reports an error
	// if the given function returns an error.
	MapKeys func(func(*K) error) error
	// Missing reports an error if the length of the map is equal to 0.
	Missing func() error
}

func Mapped

func Mapped[K comparable, V any](value *map[K]V) ReportingMapped[K, V]

Mapped identifies a map field to validate.

type ReportingNumber

type ReportingNumber[T numeric] struct {
	// Invalid reports an error if the number does not match the given
	// 'format' string. 'hints' are optional and can be used to provide
	// additional information about the specification of the format.
	Invalid func(format string, fn func(T) bool, hints ...string) error
	// Missing reports an error if the field is equal to 0.
	Missing func() error
}

func Number

func Number[T numeric](value *T) ReportingNumber[T]

Number identifies a numeric field to report on.

type ReportingObject

type ReportingObject[T any] struct {
	// Reports checks the given errors and returns a new error if any
	// of them are not nil.
	Reports func(...error) error
}

func Object

func Object[T any](value *T) ReportingObject[T]

type ReportingSliced

type ReportingSliced[T any] struct {
	// Invalid reports an error if the number does not match the given
	// 'format' string. 'hints' are optional and can be used to provide
	// additional information about the specification of the format.
	Invalid func(class string, fn func([]T) bool, hints ...string) error
	// ForEach iterates over each element in the slice and reports an
	// error if the given function returns an error.
	ForEach func(func(*T) error) error
	// Exceeds reports an error if the length of the slice is greater
	// than the given limit.
	Exceeds func(int) error
	// Missing reports an error if the length of the slice is equal to 0.
	Missing func() error
}

func Sliced

func Sliced[T any](value *[]T) ReportingSliced[T]

Sliced identifies a slice field to validate.

type ReportingString

type ReportingString[T ~string | ~[]byte] struct {
	// Invalid reports an error if the number does not match the given
	// 'format' string. 'hints' are optional and can be used to provide
	// additional information about the specification of the format.
	Invalid func(class string, fn func(T) bool, hints ...string) error
	// Exceeds reports an error if the length of the string is greater
	// than the given limit.
	Exceeds func(limit int) error
	// Missing reports an error if the field is equal to the zero value
	// of T.
	Missing func() error
}

func String

func String[T ~string | ~[]byte](value *T) ReportingString[T]

String identifies a string field to validate.

type Validator

type Validator interface {
	Validate() error
}

Validator is a value with validation. The zero value for a Validator must always return an error when validated.

Jump to

Keyboard shortcuts

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