validation

package
v7.0.1-0...-b8e8e6a Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2019 License: Apache-2.0 Imports: 5 Imported by: 11

Documentation

Index

Constants

View Source
const (
	// UDP protocol
	UDP = `UDP`
	// TCP protocol
	TCP = `TCP`
)

Variables

This section is empty.

Functions

func IsValidPortNum

func IsValidPortNum(port int) error

IsValidPortNum tests that the argument is a valid, non-zero port number.

func IsValidProtocol

func IsValidProtocol(protocol string) error

IsValidProtocol tests that the argument is TCP or UDP.

Types

type Error

type Error struct {
	Type     ErrorType
	Field    string
	BadValue interface{}
	Detail   string
}

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

func Duplicate

func Duplicate(field string, 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 string, 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 GeneralError

func GeneralError(field string, err error) *Error

GeneralError returns a *Error for a general failure. This is used to signal that an error was found that has no structured details. The err argument must be non-nil.

func InternalError

func InternalError(field string, 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 string, 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 string, 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 string, 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 string, 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 TooLong

func TooLong(field string, 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 (*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 ValidateNonnegativeField

func ValidateNonnegativeField(value int64, field string) ErrorList

ValidateNonnegativeField validates that given value is not negative.

func ValidateNonnegativeFieldFloat

func ValidateNonnegativeFieldFloat(value float64, field string) ErrorList

ValidateNonnegativeFieldFloat validates that given value is not negative.

func ValidatePort

func ValidatePort(port string, field string) ErrorList

ValidatePort validates that given value is valid and in range 0 - 65535.

func ValidatePortRange

func ValidatePortRange(portRange string, field string) (firstPort, lastPort int, allErrs ErrorList)

ValidatePortRange validates that the given value is a valid port range, with its elements in range 0 - 65535. It accepts singular ports P, and port ranges of the form N-M.

func ValidateProtocol

func ValidateProtocol(protocol string, field string) ErrorList

ValidateProtocol validates that given value belongs to supported protocols

func (ErrorList) Error

func (v ErrorList) Error() string

Error implements the error interface.

func (ErrorList) ErrorStrings

func (v ErrorList) ErrorStrings() []string

ErrorStrings returns the underlying errors as a string slice, for testing

type ErrorType

type ErrorType string

ErrorType is a machine readable value providing more detail about why a field is invalid.

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"
	// ErrorTypeGeneral is used to report general errors without additional
	// details.
	ErrorTypeGeneral ErrorType = "GeneralError"
	// ErrorTypeInternal is used to report other errors that are not related
	// to user input.  See InternalError().
	ErrorTypeInternal ErrorType = "InternalError"
)

func (ErrorType) String

func (t ErrorType) String() string

String converts a ErrorType into its corresponding canonical error message.

Jump to

Keyboard shortcuts

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