validate

package module
v0.0.0-...-dff0caf Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: MIT Imports: 4 Imported by: 0

README

Go Validate

A suite of straightforward validation functions. You put something in, you get back nil or an error.

License

See LICENSE.md

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrMustBeLonger  = NewError("must contain at least %d characters")
	ErrMustBeShorter = NewError("must contain no more than %d characters")
)
View Source
var (
	ErrMustBeGreater        = NewError("must be greater than %v")
	ErrMustBeGreaterOrEqual = NewError("must be greater than or equal to %v")
	ErrMustBeLess           = NewError("must be less than %v")
	ErrMustBeLessOrEqual    = NewError("must be less than or equal to %v")
)
View Source
var (
	ErrMustHaveMoreItems  = NewError("must have at least %d items")
	ErrMustHaveFewerItems = NewError("must have no more than %d items")
)
View Source
var (
	Err = Error{}
)

Validation error.

View Source
var (
	ErrDisallowedChars = NewError("contains disallowed characters")
)
View Source
var (
	ErrInvalidEmail = NewError("invalid email address")
)
View Source
var (
	ErrInvalidUUID = NewError("invalid UUID")
)
View Source
var (
	ErrNotEqual = NewError("must be equal to %v")
)
View Source
var (
	ErrValueNotAllowed = NewError("not allowed")
)

Functions

func All

func All[T any](fs ...func(T) error) func(T) error

All validates a value using a sequence of validation functions. If any validation function returns an error, the sequence stops and the error is returned.

Example
testAll := All(MinLength(4), Chars("0123456789abcdef"))
fmt.Println(testAll("invalid input"))
Output:

contains disallowed characters

func Chars

func Chars(allow string) func(string) error

Chars validates whether a string contains only allowed characters.

Example
testChars := Chars("0123456789abcdef")
fmt.Println(testChars("invalid input"))
Output:

contains disallowed characters

func Email

func Email(value string) error

Email validates an email address.

Example
fmt.Println(Email("not an email"))
Output:

invalid email address

func Equal

func Equal[T comparable](cmp T) func(T) error

Equal validates whether an input value is equal to a comparison value.

func ExceptChars

func ExceptChars(disallow string) func(string) error

ExceptChars validates whether a string does not contain disallowed characters.

Example
testExceptChars := Chars("0123456789abcdef")
fmt.Println(testExceptChars("invalid input"))
Output:

contains disallowed characters

func In

func In[T comparable](allow ...T) func(T) error

In validates whether a value is found in a slice of allowed values.

Example
testIn := In("abc", "def", "xyz")
fmt.Println(testIn("123"))
Output:

not allowed

func Max

func Max(n int, exclusive bool) func(int) error

Max validates whether an integer is less than or equal to a given maximum. If exclusive is true, an equal value will also produce an error.

Example
testMax := Max(10, true)
fmt.Println(testMax(10))
Output:

must be less than 10

func MaxFloat32

func MaxFloat32(n float32, exclusive bool) func(float32) error

MaxFloat32 validates whether a float32 is less than or equal to a given maximum. If exclusive is true, an equal value will also produce an error.

func MaxFloat64

func MaxFloat64(n float64, exclusive bool) func(float64) error

MaxFloat64 validates whether a float64 is less than or equal to a given maximum. If exclusive is true, an equal value will also produce an error.

func MaxLength

func MaxLength(l int) func(string) error

MaxLength validates the length of a string as being less than or equal to a given maximum.

Example
testMaxLength := MaxLength(8)
fmt.Println(testMaxLength("this string is too long"))
Output:

must contain no more than 8 characters

func MaxSize

func MaxSize[T any](l int) func([]T) error

MaxSize validates the length of a slice as being less than or equal to a given maximum.

Example
testMaxSize := MaxSize[string](3)
fmt.Println(testMaxSize([]string{"abc", "def", "ghi", "jkl"}))
Output:

must have no more than 3 items

func Min

func Min(n int, exclusive bool) func(int) error

Min validates whether an integer is less than or equal to a given maximum. If exclusive is true, an equal value will also produce an error.

Example
testMin := Min(10, false)
fmt.Println(testMin(5))
Output:

must be greater than or equal to 10

func MinFloat32

func MinFloat32(n float32, exclusive bool) func(float32) error

MinFloat32 validates whether a float32 is less than or equal to a given maximum. If exclusive is true, an equal value will also produce an error.

func MinFloat64

func MinFloat64(n float64, exclusive bool) func(float64) error

MinFloat64 validates whether a float64 is less than or equal to a given maximum. If exclusive is true, an equal value will also produce an error.

func MinLength

func MinLength(l int) func(string) error

MinLength validates the length of a string as being greater than or equal to a given minimum.

Example
testMinLength := MinLength(8)
fmt.Println(testMinLength("2short"))
Output:

must contain at least 8 characters

func MinSize

func MinSize[T any](l int) func([]T) error

MinSize validates the length of a slice as being greater than or equal to a given minimum.

Example
testMaxSize := MinSize[string](3)
fmt.Println(testMaxSize([]string{"abc", "def"}))
Output:

must have at least 3 items

func NotIn

func NotIn[T comparable](allow ...T) func(T) error

NotIn validates whether a value is not found in a slice of disallowed values.

func URL

func URL(value string) error

URL validates a URL.

Example
fmt.Println(URL("not a url"))
Output:

invalid URL

func UUID

func UUID(value string) error

UUID validates a UUID string. The UUID must be formatted with separators.

Example
fmt.Println(UUID("not a uuid"))
Output:

invalid UUID

Types

type Error

type Error struct {
	Message string
	Data    []any
}

Error represents a validation error.

var (
	ErrInvalidURL Error = NewError("invalid URL")
)

func NewError

func NewError(message string) Error

NewError creates a new validation error.

func (Error) Error

func (e Error) Error() string

Error retrieves the message of a validation Error. If it has Data, the message will be formatted.

func (Error) Is

func (e Error) Is(target error) bool

Is determines whether the Error is an instance of the target. https://pkg.go.dev/errors#Is

If the target is a validation error and specifies a message, this function returns true if the messages match. If the target is an empty validation error, this function always returns true.

func (Error) With

func (e Error) With(value any) Error

Jump to

Keyboard shortcuts

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