package field
import "k8s.io/apimachinery/pkg/util/validation/field"
Index ¶
- func NewErrorTypeMatcher(t ErrorType) utilerrors.Matcher
- type Error
- func Duplicate(field *Path, value interface{}) *Error
- func Forbidden(field *Path, detail string) *Error
- func InternalError(field *Path, err error) *Error
- func Invalid(field *Path, value interface{}, detail string) *Error
- func NotFound(field *Path, value interface{}) *Error
- func NotSupported[T ~string](field *Path, value interface{}, validValues []T) *Error
- func Required(field *Path, detail string) *Error
- func TooLong(field *Path, value interface{}, maxLength int) *Error
- func TooLongMaxLength(field *Path, value interface{}, maxLength int) *Error
- func TooMany(field *Path, actualQuantity, maxQuantity int) *Error
- func TypeInvalid(field *Path, value interface{}, detail string) *Error
- func (e *Error) Error() string
- func (e *Error) ErrorBody() string
- func (e *Error) MarkCoveredByDeclarative() *Error
- func (e *Error) WithOrigin(o string) *Error
- type ErrorList
- func (list ErrorList) ExtractCoveredByDeclarative() ErrorList
- func (list ErrorList) Filter(fns ...utilerrors.Matcher) ErrorList
- func (list ErrorList) MarkCoveredByDeclarative() ErrorList
- func (list ErrorList) RemoveCoveredByDeclarative() ErrorList
- func (list ErrorList) ToAggregate() utilerrors.Aggregate
- func (list ErrorList) WithOrigin(origin string) ErrorList
- type ErrorMatcher
- func (m ErrorMatcher) ByDetailExact() ErrorMatcher
- func (m ErrorMatcher) ByDetailRegexp() ErrorMatcher
- func (m ErrorMatcher) ByDetailSubstring() ErrorMatcher
- func (m ErrorMatcher) ByField() ErrorMatcher
- func (m ErrorMatcher) ByOrigin() ErrorMatcher
- func (m ErrorMatcher) ByType() ErrorMatcher
- func (m ErrorMatcher) ByValue() ErrorMatcher
- func (m ErrorMatcher) Exactly() ErrorMatcher
- func (m ErrorMatcher) Matches(want, got *Error) bool
- func (m ErrorMatcher) Render(e *Error) string
- func (m ErrorMatcher) RequireOriginWhenInvalid() ErrorMatcher
- func (m ErrorMatcher) Test(tb TestIntf, want, got ErrorList)
- type ErrorType
- type OmitValueType
- type Path
- func NewPath(name string, moreNames ...string) *Path
- func ToPath(opts ...PathOption) *Path
- func (p *Path) Child(name string, moreNames ...string) *Path
- func (p *Path) Index(index int) *Path
- func (p *Path) Key(key string) *Path
- func (p *Path) Root() *Path
- func (p *Path) String() string
- type PathOption
- type TestIntf
Functions ¶
func NewErrorTypeMatcher ¶
func NewErrorTypeMatcher(t ErrorType) utilerrors.Matcher
NewErrorTypeMatcher returns an errors.Matcher that returns true if the provided error is a Error and has the provided ErrorType.
Types ¶
type Error ¶
type Error struct { Type ErrorType Field string BadValue interface{} Detail string // Origin uniquely identifies where this error was generated from. It is used in testing to // compare expected errors against actual errors without relying on exact detail string matching. // This allows tests to verify the correct validation logic triggered the error // regardless of how the error message might be formatted or localized. // // The value should be either: // - A simple camelCase identifier (e.g., "maximum", "maxItems") // - A structured format using "format=<dash-style-identifier>" for validation errors related to specific formats // (e.g., "format=dns-label", "format=qualified-name") // // If the Origin corresponds to an existing declarative validation tag or JSON Schema keyword, // use that same name for consistency. // // Origin should be set in the most deeply nested validation function that // can still identify the unique source of the error. Origin string // CoveredByDeclarative is true when this error is covered by declarative // validation. This field is to identify errors from imperative validation // that should also be caught by declarative validation. CoveredByDeclarative bool }
Error is an implementation of the 'error' interface, which represents a field-level validation error.
func Duplicate ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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. If maxLength is negative, it will be included in the message. The value argument is not used.
func TooLongMaxLength ¶
TooLongMaxLength returns a *Error indicating "too long". Deprecated: Use TooLong instead.
func TooMany ¶
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 TypeInvalid ¶
TypeInvalid returns a *Error indicating "type is invalid"
func (*Error) Error ¶
Error implements the error interface.
func (*Error) ErrorBody ¶
ErrorBody returns the error message without the field name. This is useful for building nice-looking higher-level error reporting.
func (*Error) MarkCoveredByDeclarative ¶
MarkCoveredByDeclarative marks the error as covered by declarative validation.
func (*Error) WithOrigin ¶
WithOrigin adds origin information to the FieldError
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) ExtractCoveredByDeclarative ¶
ExtractCoveredByDeclarative returns a new ErrorList containing only the errors that should be covered by declarative validation.
func (ErrorList) Filter ¶
func (list ErrorList) Filter(fns ...utilerrors.Matcher) ErrorList
Filter removes items from the ErrorList that match the provided fns.
func (ErrorList) MarkCoveredByDeclarative ¶
MarkCoveredByDeclarative marks all errors in the list as covered by declarative validation.
func (ErrorList) RemoveCoveredByDeclarative ¶
RemoveCoveredByDeclarative returns a new ErrorList containing only the errors that should not be covered by declarative validation.
func (ErrorList) ToAggregate ¶
func (list ErrorList) ToAggregate() utilerrors.Aggregate
ToAggregate converts the ErrorList into an errors.Aggregate.
func (ErrorList) WithOrigin ¶
WithOrigin sets the origin for all errors in the list and returns the updated list.
type ErrorMatcher ¶
type ErrorMatcher struct {
// contains filtered or unexported fields
}
ErrorMatcher is a helper for comparing Error objects.
func (ErrorMatcher) ByDetailExact ¶
func (m ErrorMatcher) ByDetailExact() ErrorMatcher
ByDetailExact returns a derived ErrorMatcher which also matches errors by the exact detail string.
func (ErrorMatcher) ByDetailRegexp ¶
func (m ErrorMatcher) ByDetailRegexp() ErrorMatcher
ByDetailRegexp returns a derived ErrorMatcher which also matches errors by a regular expression of the detail string, where the "want" string is assumed to be a valid regular expression.
func (ErrorMatcher) ByDetailSubstring ¶
func (m ErrorMatcher) ByDetailSubstring() ErrorMatcher
ByDetailSubstring returns a derived ErrorMatcher which also matches errors by a substring of the detail string.
func (ErrorMatcher) ByField ¶
func (m ErrorMatcher) ByField() ErrorMatcher
ByField returns a derived ErrorMatcher which also matches by field path.
func (ErrorMatcher) ByOrigin ¶
func (m ErrorMatcher) ByOrigin() ErrorMatcher
ByOrigin returns a derived ErrorMatcher which also matches by the origin.
func (ErrorMatcher) ByType ¶
func (m ErrorMatcher) ByType() ErrorMatcher
ByType returns a derived ErrorMatcher which also matches by type.
func (ErrorMatcher) ByValue ¶
func (m ErrorMatcher) ByValue() ErrorMatcher
ByValue returns a derived ErrorMatcher which also matches by the errant value.
func (ErrorMatcher) Exactly ¶
func (m ErrorMatcher) Exactly() ErrorMatcher
Exactly returns a derived ErrorMatcher which matches all fields exactly.
func (ErrorMatcher) Matches ¶
func (m ErrorMatcher) Matches(want, got *Error) bool
Matches returns true if the two Error objects match according to the configured criteria.
func (ErrorMatcher) Render ¶
func (m ErrorMatcher) Render(e *Error) string
Render returns a string representation of the specified Error object, according to the criteria configured in the ErrorMatcher.
func (ErrorMatcher) RequireOriginWhenInvalid ¶
func (m ErrorMatcher) RequireOriginWhenInvalid() ErrorMatcher
RequireOriginWhenInvalid returns a derived ErrorMatcher which also requires the Origin field to be set when the Type is Invalid and the matcher is matching by Origin.
func (ErrorMatcher) Test ¶
func (m ErrorMatcher) Test(tb TestIntf, want, got ErrorList)
Test compares two ErrorLists by the criteria configured in this matcher, and fails the test if they don't match. If a given "want" error matches multiple "got" errors, they will all be consumed. This might be OK (e.g. if there are multiple errors on the same field from the same origin) or it might be an insufficiently specific matcher, so these will be logged.
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/types.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" // 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" // ErrorTypeInternal is used to report other errors that are not related // to user input. See InternalError(). ErrorTypeInternal ErrorType = "InternalError" // ErrorTypeTypeInvalid is for the value did not match the schema type for that field ErrorTypeTypeInvalid ErrorType = "FieldValueTypeInvalid" )
TODO: These values are duplicated in api/types.go, but there's a circular dep. Fix it.
func (ErrorType) String ¶
String converts a ErrorType into its corresponding canonical error message.
type OmitValueType ¶
type OmitValueType struct{}
type Path ¶
type Path struct {
// contains filtered or unexported fields
}
Path represents the path from some root to a particular field.
func NewPath ¶
NewPath creates a root Path object.
func ToPath ¶
func ToPath(opts ...PathOption) *Path
ToPath produces *Path from a set of PathOption
func (*Path) Child ¶
Child creates a new Path that is a child of the method receiver.
func (*Path) Index ¶
Index indicates that the previous Path is to be subscripted by an int. This sets the same underlying value as Key.
func (*Path) Key ¶
Key indicates that the previous Path is to be subscripted by a string. This sets the same underlying value as Index.
func (*Path) Root ¶
Root returns the root element of this Path.
func (*Path) String ¶
String produces a string representation of the Path.
type PathOption ¶
type PathOption func(o *pathOptions)
PathOption modifies a pathOptions
func WithPath ¶
func WithPath(p *Path) PathOption
WithPath generates a PathOption
type TestIntf ¶
type TestIntf interface { Helper() Errorf(format string, args ...any) Logf(format string, args ...any) }
TestIntf lets users pass a testing.T while not coupling this package to Go's testing package.
Source Files ¶
error_matcher.go errors.go path.go
- Version
- v0.33.0 (latest)
- Published
- Apr 11, 2025
- Platform
- linux/amd64
- Imports
- 8 packages
- Last checked
- 3 hours ago –
Tools for package owners.