validation

package
v0.0.0-...-b9b5e9f Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Aggregate

type Aggregate interface {
	error
	Errors() []error
	Is(error) bool
}

func NewAggregate

func NewAggregate(errlist []error) Aggregate

NewAggregate converts a slice of errors into an Aggregate interface, which is itself an implementation of the error interface. If the slice is empty, this returns nil. It will check if any of the element of input error list is nil, to avoid nil pointer panic when call Error().

type Error

type Error struct {
	Type     ErrorType   `json:"type"`
	Field    string      `json:"field"`
	BadValue interface{} `json:"badValue"`
	Detail   string      `json:"detail"`
}

Error is an implementation of the 'error' interface, which represents a field-level validation error.

func Duplicate

func Duplicate(field *Path, value interface{}) *Error

Duplicate returns a *Error indicating "duplicate value". This is used to report collisions of values that must be unique (e.g. names or IDs).

func Forbidden

func Forbidden(field *Path, detail string) *Error

Forbidden returns a *Error indicating "forbidden". This is used to report valid (as per formatting rules) values which would be accepted under some conditions, but which are not permitted by current conditions (e.g. security policy).

func InternalError

func InternalError(field *Path, err error) *Error

InternalError returns a *Error indicating "internal error". This is used to signal that an error was found that was not directly related to user input. The err argument must be non-nil.

func Invalid

func Invalid(field *Path, value interface{}, detail string) *Error

Invalid returns a *Error indicating "invalid value". This is used to report malformed values (e.g. failed regex match, too long, out of bounds).

func NotFound

func NotFound(field *Path, value interface{}) *Error

NotFound returns a *Error indicating "value not found". This is used to report failure to find a requested value (e.g. looking up an ID).

func NotSupported

func NotSupported(field *Path, value interface{}, validValues []string) *Error

NotSupported returns a *Error indicating "unsupported value". This is used to report unknown values for enumerated fields (e.g. a list of valid values).

func Required

func Required(field *Path, detail string) *Error

Required returns a *Error indicating "value required". This is used to report required values that are not provided (e.g. empty strings, null values, or empty arrays).

func TooFew

func TooFew(field *Path, actualQuantity, maxQuantity int) *Error

TooFew returns a *Error indicating "too few". This is used to report that a given list has too few items. This is similar to TooShort, but the returned error indicates quantity instead of length.

func TooLong

func TooLong(field *Path, value interface{}, maxLength int) *Error

TooLong returns a *Error indicating "too long". This is used to report that the given value is too long. This is similar to Invalid, but the returned error will not include the too-long value.

func TooMany

func TooMany(field *Path, actualQuantity, maxQuantity int) *Error

TooMany returns a *Error indicating "too many". This is used to report that a given list has too many items. This is similar to TooLong, but the returned error indicates quantity instead of length.

func TooShort

func TooShort(field *Path, value interface{}, minLength int) *Error

TooShort returns a *Error indicating "too short". This is used to report that the given value is too short. This is similar to Invalid, but the returned error will not include the too-short value.

func (*Error) Error

func (v *Error) Error() string

Error implements the error interface.

func (*Error) ErrorBody

func (v *Error) ErrorBody() string

ErrorBody returns the error message without the field name. This is useful for building nice-looking higher-level error reporting.

type ErrorList

type ErrorList []*Error

ErrorList holds a set of Errors. It is plausible that we might one day have non-field errors in this same umbrella package, but for now we don't, so we can keep it simple and leave ErrorList here.

func (ErrorList) Find

func (v ErrorList) Find(field string) *ErrorList

func (ErrorList) FindFamily

func (v ErrorList) FindFamily(field string) *ErrorList

func (ErrorList) HasError

func (v ErrorList) HasError(field string, errType ErrorType) bool

func (ErrorList) HasMany

func (v ErrorList) HasMany() bool

func (ErrorList) IsEmpty

func (v ErrorList) IsEmpty() bool

func (*ErrorList) Length

func (v *ErrorList) Length() int

func (ErrorList) Status

func (v ErrorList) Status(code int, msg string) Status

func (ErrorList) ToAggregate

func (v ErrorList) ToAggregate() Aggregate

ToAggregate converts the ErrorList into an errors.Aggregate.

type ErrorType

type ErrorType string

ErrorType is a machine readable value providing more detail about why a field is invalid. These values are expected to match 1-1 with CauseType in api/form.go.

const (
	// ErrorTypeNotFound is used to report failure to find a requested value
	// (e.g. looking up an ID).  See NotFound().
	ErrorTypeNotFound ErrorType = "FieldValueNotFound"
	// ErrorTypeRequired is used to report required values that are not
	// provided (e.g. empty strings, null values, or empty arrays).  See
	// Required().
	ErrorTypeRequired ErrorType = "FieldValueRequired"
	// ErrorTypeDuplicate is used to report collisions of values that must be
	// unique (e.g. unique IDs).  See Duplicate().
	ErrorTypeDuplicate ErrorType = "FieldValueDuplicate"
	// ErrorTypeInvalid is used to report malformed values (e.g. failed regex
	// match, too long, out of bounds).  See Invalid().
	ErrorTypeInvalid ErrorType = "FieldValueInvalid"
	// ErrorTypeNotSupported is used to report unknown values for enumerated
	// fields (e.g. a list of valid values).  See NotSupported().
	ErrorTypeNotSupported ErrorType = "FieldValueNotSupported"
	// ErrorTypeForbidden is used to report valid (as per formatting rules)
	// values which would be accepted under some conditions, but which are not
	// permitted by the current conditions (such as security policy).  See
	// Forbidden().
	ErrorTypeForbidden ErrorType = "FieldValueForbidden"
	// ErrorTypeTooLong is used to report that the given value is too long.
	// This is similar to ErrorTypeInvalid, but the error will not include the
	// too-long value.  See TooLong().
	ErrorTypeTooLong ErrorType = "FieldValueTooLong"
	// ErrorTypeTooShort is used to report that the given value is too short.
	// This is similar to ErrorTypeInvalid, but the error will not include the
	// too-short value. See TooShort().
	ErrorTypeTooShort ErrorType = "FieldValueTooShort"
	// ErrorTypeTooMany is used to report "too many". This is used to
	// report that a given list has too many items. This is similar to FieldValueTooLong,
	// but the error indicates quantity instead of length.
	ErrorTypeTooMany ErrorType = "FieldValueTooMany"
	// ErrorTypeTooFew is used to report "too few". This is used to
	// report that a given list has too few items. This is similar to FieldValueTooShort,
	// but the error indicates quantity instead of length.
	ErrorTypeTooFew ErrorType = "FieldValueTooFew"
	// ErrorTypeInternal is used to report other errors that are not related
	// to user input.  See InternalError().
	ErrorTypeInternal ErrorType = "InternalError"
)

TODO: These values are duplicated in api/form.go, but there's a circular dep. Fix it.

func (ErrorType) String

func (t ErrorType) String() string

String converts a ErrorType into its corresponding canonical error message.

type Path

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

Path represents the path from some root to a particular field.

func NewPath

func NewPath(name string, moreNames ...string) *Path

NewPath creates a root Path object.

func (*Path) Child

func (p *Path) Child(name string, moreNames ...string) *Path

Child creates a new Path that is a child of the method receiver.

func (*Path) Index

func (p *Path) Index(index int) *Path

Index indicates that the previous Path is to be subscripted by an int. This sets the same underlying value as Key.

func (*Path) Key

func (p *Path) Key(key string) *Path

Key indicates that the previous Path is to be subscripted by a string. This sets the same underlying value as Index.

func (*Path) Root

func (p *Path) Root() *Path

Root returns the root element of this Path.

func (*Path) String

func (p *Path) String() string

String produces a string representation of the Path.

type Status

type Status struct {
	Status  StatusType `json:"status"`
	Code    int        `json:"code"`
	Message string     `json:"message"`
	Errors  ErrorList  `json:"errors"`
}

func (Status) Error

func (s Status) Error() string

func (Status) Unwrap

func (s Status) Unwrap() error

type StatusType

type StatusType string
const (
	Success StatusType = "Success"
	Failure StatusType = "Failure"
)

Jump to

Keyboard shortcuts

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