validate

package
v1.20220411.3 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package validate provides helpers for validating struct values.

These can be composed to form full validation suites for nested objects using functions and interfaces instead of typical validation frameworks that use struct tags.

Index

Constants

View Source
const (
	ErrZero       ex.Class = "object should be its default value or unset"
	ErrNotZero    ex.Class = "object should not be its default value or unset"
	ErrRequired   ex.Class = "field is required"
	ErrForbidden  ex.Class = "field is forbidden"
	ErrEmpty      ex.Class = "object should be empty"
	ErrNotEmpty   ex.Class = "object should not be empty"
	ErrLen        ex.Class = "object should have a given length"
	ErrNil        ex.Class = "object should be nil"
	ErrNotNil     ex.Class = "object should not be nil"
	ErrEquals     ex.Class = "objects should be equal"
	ErrNotEquals  ex.Class = "objects should not be equal"
	ErrAllowed    ex.Class = "objects should be one of a given set of allowed values"
	ErrDisallowed ex.Class = "objects should not be one of a given set of disallowed values"
)

Basic errors

View Source
const (
	ErrFloat32Min      ex.Class = "float32 should be above a minimum value"
	ErrFloat32Max      ex.Class = "float32 should be below a maximum value"
	ErrFloat32Positive ex.Class = "float32 should be positive"
	ErrFloat32Negative ex.Class = "float32 should be negative"
	ErrFloat32Epsilon  ex.Class = "float32 should be within an epsilon of a value"
	ErrFloat32Zero     ex.Class = "float32 should be zero"
	ErrFloat32NotZero  ex.Class = "float32 should not be zero"
)

Float32 errors

View Source
const (
	ErrFloat64Min      ex.Class = "float64 should be above a minimum value"
	ErrFloat64Max      ex.Class = "float64 should be below a maximum value"
	ErrFloat64Positive ex.Class = "float64 should be positive"
	ErrFloat64Negative ex.Class = "float64 should be negative"
	ErrFloat64Epsilon  ex.Class = "float64 should be within an epsilon of a value"
	ErrFloat64Zero     ex.Class = "float64 should be zero"
	ErrFloat64NotZero  ex.Class = "float64 should not be zero"
)

Float64 errors

View Source
const (
	ErrIntMin      ex.Class = "int should be above a minimum value"
	ErrIntMax      ex.Class = "int should be below a maximum value"
	ErrIntPositive ex.Class = "int should be positive"
	ErrIntNegative ex.Class = "int should be negative"
	ErrIntZero     ex.Class = "int should be zero"
	ErrIntNotZero  ex.Class = "int should not be zero"
)

Int errors

View Source
const (
	ErrInt16Min      ex.Class = "int16 should be above a minimum value"
	ErrInt16Max      ex.Class = "int16 should be below a maximum value"
	ErrInt16Positive ex.Class = "int16 should be positive"
	ErrInt16Negative ex.Class = "int16 should be negative"
	ErrInt16Zero     ex.Class = "int16 should be zero"
	ErrInt16NotZero  ex.Class = "int16 should not be zero"
)

Int16 errors

View Source
const (
	ErrInt32Min      ex.Class = "int32 should be above a minimum value"
	ErrInt32Max      ex.Class = "int32 should be below a maximum value"
	ErrInt32Positive ex.Class = "int32 should be positive"
	ErrInt32Negative ex.Class = "int32 should be negative"
	ErrInt32Zero     ex.Class = "int32 should be zero"
	ErrInt32NotZero  ex.Class = "int32 should not be zero"
)

Int32 errors

View Source
const (
	ErrInt64Min      ex.Class = "int64 should be above a minimum value"
	ErrInt64Max      ex.Class = "int64 should be below a maximum value"
	ErrInt64Positive ex.Class = "int64 should be positive"
	ErrInt64Negative ex.Class = "int64 should be negative"
	ErrInt64Zero     ex.Class = "int64 should be zero"
	ErrInt64NotZero  ex.Class = "int64 should not be zero"
)

Int64 errors

View Source
const (
	ErrInt8Min      ex.Class = "int8 should be above a minimum value"
	ErrInt8Max      ex.Class = "int8 should be below a maximum value"
	ErrInt8Positive ex.Class = "int8 should be positive"
	ErrInt8Negative ex.Class = "int8 should be negative"
	ErrInt8Zero     ex.Class = "int8 should be zero"
	ErrInt8NotZero  ex.Class = "int8 should not be zero"
)

Int8 errors

View Source
const (
	ErrSomeNil       ex.Class = "all references should be nil"
	ErrSomeNotNil    ex.Class = "at least one reference should not be nil"
	ErrSomeOneNotNil ex.Class = "exactly one of a set of reference should not be nil"
)

Errors

View Source
const (
	ErrInstanceNotMap ex.Class = "validated reference is not a map"
	ErrMapKeys        ex.Class = "map should have keys"
)

Errors

View Source
const (
	ErrStringRequired  ex.Class = "string should be set"
	ErrStringForbidden ex.Class = "string should not be set"
	ErrStringLength    ex.Class = "string should be a given length"
	ErrStringLengthMin ex.Class = "string should be a minimum length"
	ErrStringLengthMax ex.Class = "string should be a maximum length"
	ErrStringMatches   ex.Class = "string should match regular expression"
	ErrStringIsUpper   ex.Class = "string should be uppercase"
	ErrStringIsLower   ex.Class = "string should be lowercase"
	ErrStringIsTitle   ex.Class = "string should be titlecase"
	ErrStringIsUUID    ex.Class = "string should be a uuid"
	ErrStringIsEmail   ex.Class = "string should be a valid email address"
	ErrStringIsURI     ex.Class = "string should be a valid uri"
	ErrStringIsIP      ex.Class = "string should be a valid ip address"
	ErrStringIsSlug    ex.Class = "string should be a valid slug (i.e. matching [0-9,a-z,A-Z,_,-])"
	ErrStringIsOneOf   ex.Class = "string should be one of a set of values"
)

String errors

View Source
const (
	ErrTimeBefore ex.Class = "time should be before"
	ErrTimeAfter  ex.Class = "time should be after"
)

String errors

View Source
const (
	ErrUint16Min     ex.Class = "uint16 should be above a minimum value"
	ErrUint16Max     ex.Class = "uint16 should be below a maximum value"
	ErrUint16Zero    ex.Class = "uint16 should be zero"
	ErrUint16NotZero ex.Class = "uint16 should not be zero"
)

Uint16 errors

View Source
const (
	ErrUint32Min     ex.Class = "uint32 should be above a minimum value"
	ErrUint32Max     ex.Class = "uint32 should be below a maximum value"
	ErrUint32Zero    ex.Class = "uint32 should be zero"
	ErrUint32NotZero ex.Class = "uint32 should not be zero"
)

Uint32 errors

View Source
const (
	ErrUint64Min     ex.Class = "uint64 should be above a minimum value"
	ErrUint64Max     ex.Class = "uint64 should be below a maximum value"
	ErrUint64Zero    ex.Class = "uint64 should be zero"
	ErrUint64NotZero ex.Class = "uint64 should not be zero"
)

Uint64 errors

View Source
const (
	ErrUint8Min     ex.Class = "uint8 should be above a minimum value"
	ErrUint8Max     ex.Class = "uint8 should be below a maximum value"
	ErrUint8Zero    ex.Class = "uint8 should be zero"
	ErrUint8NotZero ex.Class = "uint8 should not be zero"
)

Uint8 errors

View Source
const (
	ErrUUIDRequired  ex.Class = "uuid should be set"
	ErrUUIDForbidden ex.Class = "uuid should not be set"
	ErrUUIDV4        ex.Class = "uuid should be version 4"
	ErrUUIDVersion   ex.Class = "uuid should be a given version"
)

UUID errors

View Source
const (
	// DefaultEpsilon is a default epsilon.
	DefaultEpsilon = 0.00001
)
View Source
const (
	ErrNonLengthType ex.Class = "instance is a non-length type"
)

Errors

View Source
const (
	ErrValidation ex.Class = "validation error"
)

The root error, all validation errors inherit from this type.

Variables

This section is empty.

Functions

func ErrCause

func ErrCause(err error) error

ErrCause returns the underlying validation failure for an error. If the error is not a validation error, it returns the error class.

func ErrFormat

func ErrFormat(err error) string

ErrFormat formats an error.

func ErrMessage

func ErrMessage(err error) string

ErrMessage returns the underlying validation error message.

func ErrValue

func ErrValue(err error) interface{}

ErrValue returns the validation error value.

func Error

func Error(cause error, value interface{}, messageArgs ...interface{}) error

Error returns a new validation error. The root class of the error will be ErrValidation. The root stack will begin the frame above this call to error. The inner error will the cause of the validation vault.

func Errorf

func Errorf(cause error, value interface{}, format string, args ...interface{}) error

Errorf returns a new validation error. The root class of the error will be ErrValidation. The root stack will begin the frame above this call to error. The inner error will the cause of the validation vault.

func GetLength

func GetLength(obj interface{}) (int, error)

GetLength returns the length of an object or an error if it's not a thing that can have a length.

func Is

func Is(err error) bool

Is returns if an error is a validation error.

func IsNil

func IsNil(obj interface{}) bool

IsNil returns if an object is nil or is a typed pointer to nil.

func ReturnAll

func ReturnAll(validators ...Validator) error

ReturnAll returns all the failing validations as an array of validation errors.

func ReturnFirst

func ReturnFirst(validators ...Validator) error

ReturnFirst runs a list of validators and returns the first validator to error (if there is one).

Types

type AnyValidators

type AnyValidators struct {
	Obj interface{}
}

AnyValidators are validators for the empty interface{}.

func Any

func Any(obj interface{}) AnyValidators

Any returns a new AnyRefValidators. AnyRef can be used to validate any type, but will be more limited than using type specific validators.

func (AnyValidators) Allow

func (a AnyValidators) Allow(values ...interface{}) Validator

Allow validates a field is one of a given set of allowed values.

func (AnyValidators) Disallow

func (a AnyValidators) Disallow(values ...interface{}) Validator

Disallow validates a field is one of a given set of allowed values.

func (AnyValidators) Empty

func (a AnyValidators) Empty() Validator

Empty returns if a slice, map or channel is empty. It will error if the object is not a slice, map or channel.

func (AnyValidators) Equals

func (a AnyValidators) Equals(expected interface{}) Validator

Equals validates an object equals another object.

func (AnyValidators) Forbidden

func (a AnyValidators) Forbidden() Validator

Forbidden mirrors Zero but uses a specific error. This is useful if you want to have more aggressive failure cases.

func (AnyValidators) Len

func (a AnyValidators) Len(length int) Validator

Len validates the length is a given value.

func (AnyValidators) Nil

func (a AnyValidators) Nil() Validator

Nil validates the object is nil.

func (AnyValidators) NotEmpty

func (a AnyValidators) NotEmpty() Validator

NotEmpty returns if a slice, map or channel is not empty. It will error if the object is not a slice, map or channel.

func (AnyValidators) NotEquals

func (a AnyValidators) NotEquals(expected interface{}) Validator

NotEquals validates an object does not equal another object.

func (AnyValidators) NotNil

func (a AnyValidators) NotNil() Validator

NotNil validates the object is not nil. It also validates that the object is not an unset pointer.

func (AnyValidators) NotZero

func (a AnyValidators) NotZero() Validator

NotZero returns a validator that a given field is set. It will return an error if the field is unset. Note: this method uses reflect.Zero, there are faster .NotZero evaluators for the relevant numeric types.

func (AnyValidators) Required

func (a AnyValidators) Required() Validator

Required mirrors NotZero but uses a specific error. This is useful if you want to have more aggressive failure cases.

func (AnyValidators) Zero

func (a AnyValidators) Zero() Validator

Zero returns a validator that asserts an object is it's zero value. This nil for pointers, slices, maps, channels. And whatever equality passes for everything else with it's initialized value. Note: this method uses reflect.Zero, there are faster .Zero evaluators for the relevant numeric types.

type Float32Validators

type Float32Validators struct {
	Value *float32
}

Float32Validators implements float32 validators.

func Float32

func Float32(value *float32) Float32Validators

Float32 returns validators for float32s.

func (Float32Validators) Between

func (f Float32Validators) Between(min, max float32) Validator

Between returns a validator that a float32 is between a given min and max inclusive, that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass for [1] and [5].

func (Float32Validators) Epsilon

func (f Float32Validators) Epsilon(value, epsilon float32) Validator

Epsilon returns if a value is comparable to another value within an epsilon. It will return a failure if the absolute difference between the target value and a given value is greater than the given epsilon.

func (Float32Validators) Max

func (f Float32Validators) Max(max float32) Validator

Max returns a validator that a float32 is below a max value inclusive. Max will pass for a value 10 if the max is set to 10, that is no error would be returned.

func (Float32Validators) Min

func (f Float32Validators) Min(min float32) Validator

Min returns a validator that a float32 is above a minimum value inclusive. Min will pass for a value 1 if the min is set to 1, that is no error would be returned.

func (Float32Validators) Negative

func (f Float32Validators) Negative() Validator

Negative returns a validator that a float32 is negative.

func (Float32Validators) NotZero

func (f Float32Validators) NotZero() Validator

NotZero returns a validator that a float32 is not zero.

func (Float32Validators) Positive

func (f Float32Validators) Positive() Validator

Positive returns a validator that a float32 is positive.

func (Float32Validators) Zero

func (f Float32Validators) Zero() Validator

Zero returns a validator that a float32 is zero.

type Float64Validators

type Float64Validators struct {
	Value *float64
}

Float64Validators implements float64 validators.

func Float64

func Float64(value *float64) Float64Validators

Float64 returns validators for float64s.

func (Float64Validators) Between

func (f Float64Validators) Between(min, max float64) Validator

Between returns a validator that a float64 is between a given min and max inclusive, that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass for [1] and [5].

func (Float64Validators) Epsilon

func (f Float64Validators) Epsilon(value, epsilon float64) Validator

Epsilon returns if a value is comparable to another value within an epsilon. It will return a failure if the absolute difference between the target value and a given value is greater than the given epsilon.

func (Float64Validators) Max

func (f Float64Validators) Max(max float64) Validator

Max returns a validator that a float64 is below a max value inclusive. Max will pass for a value 10 if the max is set to 10, that is no error would be returned.

func (Float64Validators) Min

func (f Float64Validators) Min(min float64) Validator

Min returns a validator that a float64 is above a minimum value inclusive. Min will pass for a value 1 if the min is set to 1, that is no error would be returned.

func (Float64Validators) Negative

func (f Float64Validators) Negative() Validator

Negative returns a validator that a float64 is negative.

func (Float64Validators) NotZero

func (f Float64Validators) NotZero() Validator

NotZero returns a validator that a float64 is not zero.

func (Float64Validators) Positive

func (f Float64Validators) Positive() Validator

Positive returns a validator that a float64 is positive.

func (Float64Validators) Zero

func (f Float64Validators) Zero() Validator

Zero returns a validator that a float64 is zero.

type Int16Validators

type Int16Validators struct {
	Value *int16
}

Int16Validators implements int16 validators.

func Int16

func Int16(value *int16) Int16Validators

Int16 returns validators for int16s.

func (Int16Validators) Between

func (i Int16Validators) Between(min, max int16) Validator

Between returns a validator that an int16 is between a given min and max inclusive, that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass for [1] and [5].

func (Int16Validators) Max

func (i Int16Validators) Max(max int16) Validator

Max returns a validator that an int16 is below a max value inclusive. Max will pass for a value 10 if the max is set to 10, that is no error would be returned.

func (Int16Validators) Min

func (i Int16Validators) Min(min int16) Validator

Min returns a validator that an int16 is above a minimum value inclusive. Min will pass for a value 1 if the min is set to 1, that is no error would be returned.

func (Int16Validators) Negative

func (i Int16Validators) Negative() Validator

Negative returns a validator that an int16 is negative.

func (Int16Validators) NotZero

func (i Int16Validators) NotZero() Validator

NotZero returns a validator that an int16 is not zero.

func (Int16Validators) Positive

func (i Int16Validators) Positive() Validator

Positive returns a validator that an int16 is positive.

func (Int16Validators) Zero

func (i Int16Validators) Zero() Validator

Zero returns a validator that an int16 is zero.

type Int32Validators

type Int32Validators struct {
	Value *int32
}

Int32Validators implements int32 validators.

func Int32

func Int32(value *int32) Int32Validators

Int32 returns validators for int32s.

func (Int32Validators) Between

func (i Int32Validators) Between(min, max int32) Validator

Between returns a validator that an int32 is between a given min and max inclusive, that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass for [1] and [5].

func (Int32Validators) Max

func (i Int32Validators) Max(max int32) Validator

Max returns a validator that an int32 is below a max value inclusive. Max will pass for a value 10 if the max is set to 10, that is no error would be returned.

func (Int32Validators) Min

func (i Int32Validators) Min(min int32) Validator

Min returns a validator that an int32 is above a minimum value inclusive. Min will pass for a value 1 if the min is set to 1, that is no error would be returned.

func (Int32Validators) Negative

func (i Int32Validators) Negative() Validator

Negative returns a validator that an int32 is negative.

func (Int32Validators) NotZero

func (i Int32Validators) NotZero() Validator

NotZero returns a validator that an int32 is not zero.

func (Int32Validators) Positive

func (i Int32Validators) Positive() Validator

Positive returns a validator that an int32 is positive.

func (Int32Validators) Zero

func (i Int32Validators) Zero() Validator

Zero returns a validator that an int32 is zero.

type Int64Validators

type Int64Validators struct {
	Value *int64
}

Int64Validators implements int64 validators.

func Int64

func Int64(value *int64) Int64Validators

Int64 returns validators for int64s.

func (Int64Validators) Between

func (i Int64Validators) Between(min, max int64) Validator

Between returns a validator that an int64 is between a given min and max inclusive, that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass for [1] and [5].

func (Int64Validators) Max

func (i Int64Validators) Max(max int64) Validator

Max returns a validator that an int64 is below a max value inclusive. Max will pass for a value 10 if the max is set to 10, that is no error would be returned.

func (Int64Validators) Min

func (i Int64Validators) Min(min int64) Validator

Min returns a validator that an int64 is above a minimum value inclusive. Min will pass for a value 1 if the min is set to 1, that is no error would be returned.

func (Int64Validators) Negative

func (i Int64Validators) Negative() Validator

Negative returns a validator that an int64 is negative.

func (Int64Validators) NotZero

func (i Int64Validators) NotZero() Validator

NotZero returns a validator that an int64 is not zero.

func (Int64Validators) Positive

func (i Int64Validators) Positive() Validator

Positive returns a validator that an int64 is positive.

func (Int64Validators) Zero

func (i Int64Validators) Zero() Validator

Zero returns a validator that an int64 is zero.

type Int8Validators

type Int8Validators struct {
	Value *int8
}

Int8Validators implements int8 validators.

func Int8

func Int8(value *int8) Int8Validators

Int8 returns validators for int8s.

func (Int8Validators) Between

func (i Int8Validators) Between(min, max int8) Validator

Between returns a validator that an int8 is between a given min and max inclusive, that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass for [1] and [5].

func (Int8Validators) Max

func (i Int8Validators) Max(max int8) Validator

Max returns a validator that an int8 is below a max value inclusive. Max will pass for a value 10 if the max is set to 10, that is no error would be returned.

func (Int8Validators) Min

func (i Int8Validators) Min(min int8) Validator

Min returns a validator that an int8 is above a minimum value inclusive. Min will pass for a value 1 if the min is set to 1, that is no error would be returned.

func (Int8Validators) Negative

func (i Int8Validators) Negative() Validator

Negative returns a validator that an int8 is negative.

func (Int8Validators) NotZero

func (i Int8Validators) NotZero() Validator

NotZero returns a validator that an int8 is not zero.

func (Int8Validators) Positive

func (i Int8Validators) Positive() Validator

Positive returns a validator that an int8 is positive.

func (Int8Validators) Zero

func (i Int8Validators) Zero() Validator

Zero returns a validator that an int8 is zero.

type IntValidators

type IntValidators struct {
	Value *int
}

IntValidators implements int validators.

func Int

func Int(value *int) IntValidators

Int returns validators for ints.

func (IntValidators) Between

func (i IntValidators) Between(min, max int) Validator

Between returns a validator that an int is between a given min and max inclusive, that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass for [1] and [5].

func (IntValidators) Max

func (i IntValidators) Max(max int) Validator

Max returns a validator that an int is below a max value inclusive. Max will pass for a value 10 if the max is set to 10, that is no error would be returned.

func (IntValidators) Min

func (i IntValidators) Min(min int) Validator

Min returns a validator that an int is above a minimum value inclusive. Min will pass for a value 1 if the min is set to 1, that is no error would be returned.

func (IntValidators) Negative

func (i IntValidators) Negative() Validator

Negative returns a validator that an int is negative.

func (IntValidators) NotZero

func (i IntValidators) NotZero() Validator

NotZero returns a validator that an int is not zero.

func (IntValidators) Positive

func (i IntValidators) Positive() Validator

Positive returns a validator that an int is positive.

func (IntValidators) Zero

func (i IntValidators) Zero() Validator

Zero returns a validator that an int is zero.

type ManyValidators

type ManyValidators struct {
	Objs []interface{}
}

ManyValidators returns the validator singleton for some rules.

func Many

func Many(objs ...interface{}) ManyValidators

Many defines validator rules that apply to a variadic set of untyped references.

func (ManyValidators) Nil

func (mv ManyValidators) Nil() Validator

Nil ensures none of a set of references aren't nil.

func (ManyValidators) NotNil

func (mv ManyValidators) NotNil() Validator

NotNil ensures at least one reference is set.

func (ManyValidators) OneNotNil

func (mv ManyValidators) OneNotNil() Validator

OneNotNil ensures at least and at most one reference is set.

type MapValidators

type MapValidators struct {
	Value interface{}
}

MapValidators is a set of validators for maps.

func Map

func Map(instance interface{}) MapValidators

Map returns validators for a map type reference.

func (MapValidators) Keys

func (mv MapValidators) Keys(keys ...interface{}) Validator

Keys validates a map contains a given set of keys.

type NoOp

type NoOp struct{}

NoOp is an empty validated.

func (NoOp) Validate

func (no NoOp) Validate() error

Validate implements the no op.

type StringValidators

type StringValidators struct {
	Value *string
}

StringValidators returns string validators.

func String

func String(value *string) StringValidators

String contains helpers for string validation.

func (StringValidators) BetweenLen

func (s StringValidators) BetweenLen(min, max int) Validator

BetweenLen returns a validator that a string is a between a minimum and maximum length. It will error if the string is unset (nil).

func (StringValidators) Forbidden

func (s StringValidators) Forbidden() Validator

Forbidden returns a validator that a string is not set.

func (StringValidators) IsEmail

func (s StringValidators) IsEmail() Validator

IsEmail returns if a string is a valid email address.

func (StringValidators) IsIP

func (s StringValidators) IsIP() Validator

IsIP returns if a string is a valid ip address. It will error if the string is unset (nil).

func (StringValidators) IsLower

func (s StringValidators) IsLower() Validator

IsLower returns a validator if a string is all lowercase. It will error if the string is unset (nil).

func (StringValidators) IsOneOf

func (s StringValidators) IsOneOf(values ...string) Validator

IsOneOf validates a string is one of a known set of values.

func (StringValidators) IsSlug

func (s StringValidators) IsSlug() Validator

IsSlug returns if a string is a valid slug as defined by the match rule [0-9,a-z,A-Z,_,-]. It will error if the string is unset (nil).

func (StringValidators) IsTitle

func (s StringValidators) IsTitle() Validator

IsTitle returns a validator if a string is titlecase. Titlecase is defined as the output of strings.ToTitle(s). It will error if the string is unset (nil).

func (StringValidators) IsURI

func (s StringValidators) IsURI() Validator

IsURI returns if a string is a valid uri. It will error if the string is unset (nil).

func (StringValidators) IsUUID

func (s StringValidators) IsUUID() Validator

IsUUID returns if a string is a valid uuid. It will error if the string is unset (nil).

func (StringValidators) IsUpper

func (s StringValidators) IsUpper() Validator

IsUpper returns a validator if a string is all uppercase. It will error if the string is unset (nil).

func (StringValidators) Length

func (s StringValidators) Length(length int) Validator

Length returns a validator that a string is a minimum length. It will error if the string is unset (nil).

func (StringValidators) Matches

func (s StringValidators) Matches(expression string) Validator

Matches returns a validator that a string matches a given regex. It will error if the string is unset (nil).

func (StringValidators) MaxLen

func (s StringValidators) MaxLen(length int) Validator

MaxLen returns a validator that a string is a minimum length. It will pass if the string is unset (nil).

func (StringValidators) MinLen

func (s StringValidators) MinLen(length int) Validator

MinLen returns a validator that a string is a minimum length. If the string is unset (nil) it will fail.

func (StringValidators) Required

func (s StringValidators) Required() Validator

Required returns a validator that a string is set and not zero length.

type TimeValidators

type TimeValidators struct {
	Value *time.Time
}

TimeValidators implements validators for time.Time values.

func Time

func Time(value *time.Time) TimeValidators

Time validator singleton.

func (TimeValidators) After

func (t TimeValidators) After(after time.Time) Validator

After returns a validator that a time should be after a given time.

func (TimeValidators) AfterNowUTC

func (t TimeValidators) AfterNowUTC() Validator

AfterNowUTC returns a validator that a time should be after a given time.

func (TimeValidators) Before

func (t TimeValidators) Before(before time.Time) Validator

Before returns a validator that a time should be before a given time.

func (TimeValidators) BeforeNowUTC

func (t TimeValidators) BeforeNowUTC() Validator

BeforeNowUTC returns a validator that a time should be before a given time.

func (TimeValidators) Between

func (t TimeValidators) Between(start, end time.Time) Validator

Between returns a validator that a time should be after a given time.

type UUIDValidators

type UUIDValidators struct {
	Value *uuid.UUID
}

UUIDValidators implements validators for uuid.UUIDs.

func UUID

func UUID(value *uuid.UUID) UUIDValidators

UUID returns a uuid.UUID validator.

func (UUIDValidators) Forbidden

func (u UUIDValidators) Forbidden() Validator

Forbidden returns a validator that an uuid is not set.

func (UUIDValidators) IsV4

func (u UUIDValidators) IsV4() Validator

IsV4 returns a validator that asserts a uuid.UUID is a V4 uuid.

func (UUIDValidators) IsVersion

func (u UUIDValidators) IsVersion(version byte) Validator

IsVersion returns a validator that asserts a uuid.UUID is a given version.

func (UUIDValidators) Required

func (u UUIDValidators) Required() Validator

Required returns a validator that an uuid is set.

type Uint16Validators

type Uint16Validators struct {
	Value *uint16
}

Uint16Validators implements uint16 validators.

func Uint16

func Uint16(value *uint16) Uint16Validators

Uint16 returns validators for uint16s.

func (Uint16Validators) Between

func (i Uint16Validators) Between(min, max uint16) Validator

Between returns a validator that an uint16 is between a given min and max inclusive, that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass for [1] and [5].

func (Uint16Validators) Max

func (i Uint16Validators) Max(max uint16) Validator

Max returns a validator that an uint16 is below a max value inclusive. Max will pass for a value 10 if the max is set to 10, that is no error would be returned.

func (Uint16Validators) Min

func (i Uint16Validators) Min(min uint16) Validator

Min returns a validator that an uint16 is above a minimum value inclusive. Min will pass for a value 1 if the min is set to 1, that is no error would be returned.

func (Uint16Validators) NotZero

func (i Uint16Validators) NotZero() Validator

NotZero returns a validator that an uint16 is not zero.

func (Uint16Validators) Zero

func (i Uint16Validators) Zero() Validator

Zero returns a validator that an uint16 is zero.

type Uint32Validators

type Uint32Validators struct {
	Value *uint32
}

Uint32Validators implements uint32 validators.

func Uint32

func Uint32(value *uint32) Uint32Validators

Uint32 returns validators for uint32s.

func (Uint32Validators) Between

func (i Uint32Validators) Between(min, max uint32) Validator

Between returns a validator that an uint32 is between a given min and max inclusive, that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass for [1] and [5].

func (Uint32Validators) Max

func (i Uint32Validators) Max(max uint32) Validator

Max returns a validator that an uint32 is below a max value inclusive. Max will pass for a value 10 if the max is set to 10, that is no error would be returned.

func (Uint32Validators) Min

func (i Uint32Validators) Min(min uint32) Validator

Min returns a validator that an uint32 is above a minimum value inclusive. Min will pass for a value 1 if the min is set to 1, that is no error would be returned.

func (Uint32Validators) NotZero

func (i Uint32Validators) NotZero() Validator

NotZero returns a validator that an uint32 is not zero.

func (Uint32Validators) Zero

func (i Uint32Validators) Zero() Validator

Zero returns a validator that an uint32 is zero.

type Uint64Validators

type Uint64Validators struct {
	Value *uint64
}

Uint64Validators implements uint64 validators.

func Uint64

func Uint64(value *uint64) Uint64Validators

Uint64 returns validators for uint64s.

func (Uint64Validators) Between

func (i Uint64Validators) Between(min, max uint64) Validator

Between returns a validator that an uint64 is between a given min and max inclusive, that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass for [1] and [5].

func (Uint64Validators) Max

func (i Uint64Validators) Max(max uint64) Validator

Max returns a validator that an uint64 is below a max value inclusive. Max will pass for a value 10 if the max is set to 10, that is no error would be returned.

func (Uint64Validators) Min

func (i Uint64Validators) Min(min uint64) Validator

Min returns a validator that an uint64 is above a minimum value inclusive. Min will pass for a value 1 if the min is set to 1, that is no error would be returned.

func (Uint64Validators) NotZero

func (i Uint64Validators) NotZero() Validator

NotZero returns a validator that an uint64 is not zero.

func (Uint64Validators) Zero

func (i Uint64Validators) Zero() Validator

Zero returns a validator that an uint64 is zero.

type Uint8Validators

type Uint8Validators struct {
	Value *uint8
}

Uint8Validators implements uint8 validators.

func Uint8

func Uint8(value *uint8) Uint8Validators

Uint8 returns validators for uint8s.

func (Uint8Validators) Between

func (i Uint8Validators) Between(min, max uint8) Validator

Between returns a validator that an uint8 is between a given min and max inclusive, that is, `.Between(1,5)` will _fail_ for [0] and [6] respectively, but pass for [1] and [5].

func (Uint8Validators) Max

func (i Uint8Validators) Max(max uint8) Validator

Max returns a validator that an uint8 is below a max value inclusive. Max will pass for a value 10 if the max is set to 10, that is no error would be returned.

func (Uint8Validators) Min

func (i Uint8Validators) Min(min uint8) Validator

Min returns a validator that an uint8 is above a minimum value inclusive. Min will pass for a value 1 if the min is set to 1, that is no error would be returned.

func (Uint8Validators) NotZero

func (i Uint8Validators) NotZero() Validator

NotZero returns a validator that an uint8 is not zero.

func (Uint8Validators) Zero

func (i Uint8Validators) Zero() Validator

Zero returns a validator that an uint8 is zero.

type ValidationError

type ValidationError struct {
	// Cause is the error class that connotes the type of failure.
	Cause error
	// Message is variable or contextaul data to add meaning to the cause.
	Message string
	// Value is the offending value, it can be unset, and is meant to be a common piece of context.
	Value interface{}
}

ValidationError is the inner error for validation exceptions.

func ErrInner

func ErrInner(err error) *ValidationError

ErrInner returns the inner validation error if it's present on the outer error.

func (ValidationError) Class

func (ve ValidationError) Class() error

Class implements

func (ValidationError) Error

func (ve ValidationError) Error() string

Error implements error.

type ValidationErrors

type ValidationErrors []error

ValidationErrors is a set of errors.

func (ValidationErrors) Error

func (ve ValidationErrors) Error() string

Error implements error.

type Validator

type Validator func() error

Validator validates any object.

func All

func All(validators ...Validator) Validator

All returns a validator that returns all non-nil validation errors from a given set of validators.

func First

func First(validators ...Validator) Validator

First is a validator that returns the first error of a given set of validators.

func When

func When(predicate func() bool, passes Validator) Validator

When returns the result of the "passes" validator if the predicate returns true, otherwise it returns nil.

func WhenElse

func WhenElse(predicate func() bool, passes, fails Validator) Validator

WhenElse returns the result of the "passes" validator if the predicate returns true, otherwise it returns the result of the "fails" validator.

Jump to

Keyboard shortcuts

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