Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrFieldRequired specifies the case where a required field isn't populated at use time. ErrFieldRequired = errors.New("field is required") // ErrFieldInvalid specifies the case where a field isn't populated in a valid manner. ErrFieldInvalid = errors.New("field is invalid") // ErrFieldEnumInvalid specifies the case where the given value isn't part of the known values in the enum. ErrFieldEnumInvalid = errors.New("field value isn't known to this enum") )
Functions ¶
func TestExpectErrors ¶
TestExpectErrors loops through all expected errors and make sure that errors.Is returns true for all of them. If there aren't any expected errors, and err != nil, an error will be reported too.
func ValidateTargets ¶
func ValidateTargets(name string, targets ...ValidateTarget) error
ValidateTargets runs the ValidateFields() method for each of the targets, and returns the aggregate error.
Types ¶
type MultiError ¶
type MultiError struct {
Errors []error
}
MultiError is a holder struct for multiple errors returned at once Each of the errors might wrap their own underlying error. In order to check whether an error returned from a function was a *MultiError, you can do:
multiErr := &MultiError{} if errors.Is(err, multiErr) { // do things }
In order to get the value of the *MultiError (embedded somewhere in the chain, in order to access the sub-errors), you can do:
multiErr := &MultiError{} if errors.As(err, &multiErr) { // multiErr contains sub-errors, do things }
It is also possible to access sub-errors from a MultiError directly, using errors.As and errors.Is. Example:
multiErr := &MultiError{Errors: []error{ErrFieldRequired, ErrFieldInvalid}} if errors.Is(multiErr, ErrFieldInvalid) { // will return true, as ErrFieldInvalid is contained } type customError struct { data string } func (e *customError) Error() string { return "custom" + data } multiErr := &MultiError{Errors: []error{ErrFieldRequired, &customError{"my-value"}}} target := &customError{} if errors.As(multiErr, &target) { // target.data will now be "my-value" }
func NewMultiError ¶
func NewMultiError(errs ...error) *MultiError
NewMultiError returns a new *MultiError instance for the given errors.
func (*MultiError) As ¶
func (e *MultiError) As(target interface{}) bool
As implements the interface used by errors.As in order to get the value of an embedded struct error of this MultiError.
func (*MultiError) Error ¶
func (e *MultiError) Error() string
Error implements the error interface on the pointer type of MultiError.Error. This enforces callers to always return &MultiError{} for consistency.
func (*MultiError) Is ¶
func (e *MultiError) Is(target error) bool
Is implements the interface used by errors.Is in order to check if two errors are the same. This function recursively checks all contained errors.
type ValidateTarget ¶
type ValidateTarget interface { // ValidateFields registers any validation errors into the validator ValidateFields(v Validator) }
ValidateTarget is an interface for structs that aren't top-level Objects, i.e. implementing higher-level interfaces. Nested structs might instead want to implement this interface, to be able to tell a Validator if this instance is ok or contains errors.
type Validator ¶
type Validator interface { // Append registers a validation error in the internal list, capturing the value and the field that // caused the problem. Append(err error, value interface{}, fieldPaths ...string) // Invalid is a helper method for Append, registering ErrFieldInvalid as the cause, along with what field // caused the error. fieldPaths should contain the names of all nested sub-fields (of the struct) that caused // the error. Specifying the value that was invalid is also supported Invalid(value interface{}, fieldPaths ...string) // Required is a helper method for Append, registering ErrFieldRequired as the cause, along with what field // caused the error. fieldPaths should contain the names of all nested sub-fields (of the struct) that caused // the error. Required(fieldPaths ...string) // Error returns an aggregated error (or nil), based on the errors that have been registered // A *MultiError is returned if there are multiple errors. Users of this function might use // multiErr := &MultiError{}; errors.As(err, &multiErr) or errors.Is(err, multiErr) to detect // that many errors were returned Error() error }
Validator is an interface that helps with validating objects.