mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
s/ValidationErrorType/ErrorType/
This commit is contained in:
parent
0ff66da346
commit
2a127d089a
@ -536,30 +536,30 @@ func TestValidateVolumes(t *testing.T) {
|
|||||||
slashInName := api.VolumeSource{Flocker: &api.FlockerVolumeSource{DatasetName: "foo/bar"}}
|
slashInName := api.VolumeSource{Flocker: &api.FlockerVolumeSource{DatasetName: "foo/bar"}}
|
||||||
errorCases := map[string]struct {
|
errorCases := map[string]struct {
|
||||||
V []api.Volume
|
V []api.Volume
|
||||||
T validation.ValidationErrorType
|
T validation.ErrorType
|
||||||
F string
|
F string
|
||||||
D string
|
D string
|
||||||
}{
|
}{
|
||||||
"zero-length name": {[]api.Volume{{Name: "", VolumeSource: emptyVS}}, validation.ValidationErrorTypeRequired, "[0].name", ""},
|
"zero-length name": {[]api.Volume{{Name: "", VolumeSource: emptyVS}}, validation.ErrorTypeRequired, "[0].name", ""},
|
||||||
"name > 63 characters": {[]api.Volume{{Name: strings.Repeat("a", 64), VolumeSource: emptyVS}}, validation.ValidationErrorTypeInvalid, "[0].name", "must be a DNS label (at most 63 characters, matching regex [a-z0-9]([-a-z0-9]*[a-z0-9])?): e.g. \"my-name\""},
|
"name > 63 characters": {[]api.Volume{{Name: strings.Repeat("a", 64), VolumeSource: emptyVS}}, validation.ErrorTypeInvalid, "[0].name", "must be a DNS label (at most 63 characters, matching regex [a-z0-9]([-a-z0-9]*[a-z0-9])?): e.g. \"my-name\""},
|
||||||
"name not a DNS label": {[]api.Volume{{Name: "a.b.c", VolumeSource: emptyVS}}, validation.ValidationErrorTypeInvalid, "[0].name", "must be a DNS label (at most 63 characters, matching regex [a-z0-9]([-a-z0-9]*[a-z0-9])?): e.g. \"my-name\""},
|
"name not a DNS label": {[]api.Volume{{Name: "a.b.c", VolumeSource: emptyVS}}, validation.ErrorTypeInvalid, "[0].name", "must be a DNS label (at most 63 characters, matching regex [a-z0-9]([-a-z0-9]*[a-z0-9])?): e.g. \"my-name\""},
|
||||||
"name not unique": {[]api.Volume{{Name: "abc", VolumeSource: emptyVS}, {Name: "abc", VolumeSource: emptyVS}}, validation.ValidationErrorTypeDuplicate, "[1].name", ""},
|
"name not unique": {[]api.Volume{{Name: "abc", VolumeSource: emptyVS}, {Name: "abc", VolumeSource: emptyVS}}, validation.ErrorTypeDuplicate, "[1].name", ""},
|
||||||
"empty portal": {[]api.Volume{{Name: "badportal", VolumeSource: emptyPortal}}, validation.ValidationErrorTypeRequired, "[0].source.iscsi.targetPortal", ""},
|
"empty portal": {[]api.Volume{{Name: "badportal", VolumeSource: emptyPortal}}, validation.ErrorTypeRequired, "[0].source.iscsi.targetPortal", ""},
|
||||||
"empty iqn": {[]api.Volume{{Name: "badiqn", VolumeSource: emptyIQN}}, validation.ValidationErrorTypeRequired, "[0].source.iscsi.iqn", ""},
|
"empty iqn": {[]api.Volume{{Name: "badiqn", VolumeSource: emptyIQN}}, validation.ErrorTypeRequired, "[0].source.iscsi.iqn", ""},
|
||||||
"empty hosts": {[]api.Volume{{Name: "badhost", VolumeSource: emptyHosts}}, validation.ValidationErrorTypeRequired, "[0].source.glusterfs.endpoints", ""},
|
"empty hosts": {[]api.Volume{{Name: "badhost", VolumeSource: emptyHosts}}, validation.ErrorTypeRequired, "[0].source.glusterfs.endpoints", ""},
|
||||||
"empty path": {[]api.Volume{{Name: "badpath", VolumeSource: emptyPath}}, validation.ValidationErrorTypeRequired, "[0].source.glusterfs.path", ""},
|
"empty path": {[]api.Volume{{Name: "badpath", VolumeSource: emptyPath}}, validation.ErrorTypeRequired, "[0].source.glusterfs.path", ""},
|
||||||
"empty datasetName": {[]api.Volume{{Name: "badname", VolumeSource: emptyName}}, validation.ValidationErrorTypeRequired, "[0].source.flocker.datasetName", ""},
|
"empty datasetName": {[]api.Volume{{Name: "badname", VolumeSource: emptyName}}, validation.ErrorTypeRequired, "[0].source.flocker.datasetName", ""},
|
||||||
"empty mon": {[]api.Volume{{Name: "badmon", VolumeSource: emptyMon}}, validation.ValidationErrorTypeRequired, "[0].source.rbd.monitors", ""},
|
"empty mon": {[]api.Volume{{Name: "badmon", VolumeSource: emptyMon}}, validation.ErrorTypeRequired, "[0].source.rbd.monitors", ""},
|
||||||
"empty image": {[]api.Volume{{Name: "badimage", VolumeSource: emptyImage}}, validation.ValidationErrorTypeRequired, "[0].source.rbd.image", ""},
|
"empty image": {[]api.Volume{{Name: "badimage", VolumeSource: emptyImage}}, validation.ErrorTypeRequired, "[0].source.rbd.image", ""},
|
||||||
"empty cephfs mon": {[]api.Volume{{Name: "badmon", VolumeSource: emptyCephFSMon}}, validation.ValidationErrorTypeRequired, "[0].source.cephfs.monitors", ""},
|
"empty cephfs mon": {[]api.Volume{{Name: "badmon", VolumeSource: emptyCephFSMon}}, validation.ErrorTypeRequired, "[0].source.cephfs.monitors", ""},
|
||||||
"empty metatada path": {[]api.Volume{{Name: "emptyname", VolumeSource: emptyPathName}}, validation.ValidationErrorTypeRequired, "[0].source.downwardApi.path", ""},
|
"empty metatada path": {[]api.Volume{{Name: "emptyname", VolumeSource: emptyPathName}}, validation.ErrorTypeRequired, "[0].source.downwardApi.path", ""},
|
||||||
"absolute path": {[]api.Volume{{Name: "absolutepath", VolumeSource: absolutePathName}}, validation.ValidationErrorTypeForbidden, "[0].source.downwardApi.path", ""},
|
"absolute path": {[]api.Volume{{Name: "absolutepath", VolumeSource: absolutePathName}}, validation.ErrorTypeForbidden, "[0].source.downwardApi.path", ""},
|
||||||
"dot dot path": {[]api.Volume{{Name: "dotdotpath", VolumeSource: dotDotInPath}}, validation.ValidationErrorTypeInvalid, "[0].source.downwardApi.path", "must not contain \"..\"."},
|
"dot dot path": {[]api.Volume{{Name: "dotdotpath", VolumeSource: dotDotInPath}}, validation.ErrorTypeInvalid, "[0].source.downwardApi.path", "must not contain \"..\"."},
|
||||||
"dot dot file name": {[]api.Volume{{Name: "dotdotfilename", VolumeSource: dotDotPathName}}, validation.ValidationErrorTypeInvalid, "[0].source.downwardApi.path", "must not start with \"..\"."},
|
"dot dot file name": {[]api.Volume{{Name: "dotdotfilename", VolumeSource: dotDotPathName}}, validation.ErrorTypeInvalid, "[0].source.downwardApi.path", "must not start with \"..\"."},
|
||||||
"dot dot first level dirent": {[]api.Volume{{Name: "dotdotdirfilename", VolumeSource: dotDotFirstLevelDirent}}, validation.ValidationErrorTypeInvalid, "[0].source.downwardApi.path", "must not start with \"..\"."},
|
"dot dot first level dirent": {[]api.Volume{{Name: "dotdotdirfilename", VolumeSource: dotDotFirstLevelDirent}}, validation.ErrorTypeInvalid, "[0].source.downwardApi.path", "must not start with \"..\"."},
|
||||||
"empty wwn": {[]api.Volume{{Name: "badimage", VolumeSource: zeroWWN}}, validation.ValidationErrorTypeRequired, "[0].source.fc.targetWWNs", ""},
|
"empty wwn": {[]api.Volume{{Name: "badimage", VolumeSource: zeroWWN}}, validation.ErrorTypeRequired, "[0].source.fc.targetWWNs", ""},
|
||||||
"empty lun": {[]api.Volume{{Name: "badimage", VolumeSource: emptyLun}}, validation.ValidationErrorTypeRequired, "[0].source.fc.lun", ""},
|
"empty lun": {[]api.Volume{{Name: "badimage", VolumeSource: emptyLun}}, validation.ErrorTypeRequired, "[0].source.fc.lun", ""},
|
||||||
"slash in datasetName": {[]api.Volume{{Name: "slashinname", VolumeSource: slashInName}}, validation.ValidationErrorTypeInvalid, "[0].source.flocker.datasetName", "must not contain '/'"},
|
"slash in datasetName": {[]api.Volume{{Name: "slashinname", VolumeSource: slashInName}}, validation.ErrorTypeInvalid, "[0].source.flocker.datasetName", "must not contain '/'"},
|
||||||
}
|
}
|
||||||
for k, v := range errorCases {
|
for k, v := range errorCases {
|
||||||
_, errs := validateVolumes(v.V)
|
_, errs := validateVolumes(v.V)
|
||||||
@ -603,23 +603,23 @@ func TestValidatePorts(t *testing.T) {
|
|||||||
|
|
||||||
errorCases := map[string]struct {
|
errorCases := map[string]struct {
|
||||||
P []api.ContainerPort
|
P []api.ContainerPort
|
||||||
T validation.ValidationErrorType
|
T validation.ErrorType
|
||||||
F string
|
F string
|
||||||
D string
|
D string
|
||||||
}{
|
}{
|
||||||
"name > 15 characters": {[]api.ContainerPort{{Name: strings.Repeat("a", 16), ContainerPort: 80, Protocol: "TCP"}}, validation.ValidationErrorTypeInvalid, "[0].name", PortNameErrorMsg},
|
"name > 15 characters": {[]api.ContainerPort{{Name: strings.Repeat("a", 16), ContainerPort: 80, Protocol: "TCP"}}, validation.ErrorTypeInvalid, "[0].name", PortNameErrorMsg},
|
||||||
"name not a IANA svc name ": {[]api.ContainerPort{{Name: "a.b.c", ContainerPort: 80, Protocol: "TCP"}}, validation.ValidationErrorTypeInvalid, "[0].name", PortNameErrorMsg},
|
"name not a IANA svc name ": {[]api.ContainerPort{{Name: "a.b.c", ContainerPort: 80, Protocol: "TCP"}}, validation.ErrorTypeInvalid, "[0].name", PortNameErrorMsg},
|
||||||
"name not a IANA svc name (i.e. a number)": {[]api.ContainerPort{{Name: "80", ContainerPort: 80, Protocol: "TCP"}}, validation.ValidationErrorTypeInvalid, "[0].name", PortNameErrorMsg},
|
"name not a IANA svc name (i.e. a number)": {[]api.ContainerPort{{Name: "80", ContainerPort: 80, Protocol: "TCP"}}, validation.ErrorTypeInvalid, "[0].name", PortNameErrorMsg},
|
||||||
"name not unique": {[]api.ContainerPort{
|
"name not unique": {[]api.ContainerPort{
|
||||||
{Name: "abc", ContainerPort: 80, Protocol: "TCP"},
|
{Name: "abc", ContainerPort: 80, Protocol: "TCP"},
|
||||||
{Name: "abc", ContainerPort: 81, Protocol: "TCP"},
|
{Name: "abc", ContainerPort: 81, Protocol: "TCP"},
|
||||||
}, validation.ValidationErrorTypeDuplicate, "[1].name", ""},
|
}, validation.ErrorTypeDuplicate, "[1].name", ""},
|
||||||
"zero container port": {[]api.ContainerPort{{ContainerPort: 0, Protocol: "TCP"}}, validation.ValidationErrorTypeInvalid, "[0].containerPort", PortRangeErrorMsg},
|
"zero container port": {[]api.ContainerPort{{ContainerPort: 0, Protocol: "TCP"}}, validation.ErrorTypeInvalid, "[0].containerPort", PortRangeErrorMsg},
|
||||||
"invalid container port": {[]api.ContainerPort{{ContainerPort: 65536, Protocol: "TCP"}}, validation.ValidationErrorTypeInvalid, "[0].containerPort", PortRangeErrorMsg},
|
"invalid container port": {[]api.ContainerPort{{ContainerPort: 65536, Protocol: "TCP"}}, validation.ErrorTypeInvalid, "[0].containerPort", PortRangeErrorMsg},
|
||||||
"invalid host port": {[]api.ContainerPort{{ContainerPort: 80, HostPort: 65536, Protocol: "TCP"}}, validation.ValidationErrorTypeInvalid, "[0].hostPort", PortRangeErrorMsg},
|
"invalid host port": {[]api.ContainerPort{{ContainerPort: 80, HostPort: 65536, Protocol: "TCP"}}, validation.ErrorTypeInvalid, "[0].hostPort", PortRangeErrorMsg},
|
||||||
"invalid protocol case": {[]api.ContainerPort{{ContainerPort: 80, Protocol: "tcp"}}, validation.ValidationErrorTypeNotSupported, "[0].protocol", "supported values: TCP, UDP"},
|
"invalid protocol case": {[]api.ContainerPort{{ContainerPort: 80, Protocol: "tcp"}}, validation.ErrorTypeNotSupported, "[0].protocol", "supported values: TCP, UDP"},
|
||||||
"invalid protocol": {[]api.ContainerPort{{ContainerPort: 80, Protocol: "ICMP"}}, validation.ValidationErrorTypeNotSupported, "[0].protocol", "supported values: TCP, UDP"},
|
"invalid protocol": {[]api.ContainerPort{{ContainerPort: 80, Protocol: "ICMP"}}, validation.ErrorTypeNotSupported, "[0].protocol", "supported values: TCP, UDP"},
|
||||||
"protocol required": {[]api.ContainerPort{{Name: "abc", ContainerPort: 80}}, validation.ValidationErrorTypeRequired, "[0].protocol", ""},
|
"protocol required": {[]api.ContainerPort{{Name: "abc", ContainerPort: 80}}, validation.ErrorTypeRequired, "[0].protocol", ""},
|
||||||
}
|
}
|
||||||
for k, v := range errorCases {
|
for k, v := range errorCases {
|
||||||
errs := validatePorts(v.P)
|
errs := validatePorts(v.P)
|
||||||
@ -3764,7 +3764,7 @@ func TestValidateEndpoints(t *testing.T) {
|
|||||||
|
|
||||||
errorCases := map[string]struct {
|
errorCases := map[string]struct {
|
||||||
endpoints api.Endpoints
|
endpoints api.Endpoints
|
||||||
errorType validation.ValidationErrorType
|
errorType validation.ErrorType
|
||||||
errorDetail string
|
errorDetail string
|
||||||
}{
|
}{
|
||||||
"missing namespace": {
|
"missing namespace": {
|
||||||
@ -4002,7 +4002,7 @@ func TestValidateSecurityContext(t *testing.T) {
|
|||||||
|
|
||||||
errorCases := map[string]struct {
|
errorCases := map[string]struct {
|
||||||
sc *api.SecurityContext
|
sc *api.SecurityContext
|
||||||
errorType validation.ValidationErrorType
|
errorType validation.ErrorType
|
||||||
errorDetail string
|
errorDetail string
|
||||||
}{
|
}{
|
||||||
"request privileged when capabilities forbids": {
|
"request privileged when capabilities forbids": {
|
||||||
|
@ -25,52 +25,52 @@ import (
|
|||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ValidationErrorType is a machine readable value providing more detail about why
|
// ErrorType is a machine readable value providing more detail about why
|
||||||
// a field is invalid. These values are expected to match 1-1 with
|
// a field is invalid. These values are expected to match 1-1 with
|
||||||
// CauseType in api/types.go.
|
// CauseType in api/types.go.
|
||||||
type ValidationErrorType string
|
type ErrorType string
|
||||||
|
|
||||||
// TODO: These values are duplicated in api/types.go, but there's a circular dep. Fix it.
|
// TODO: These values are duplicated in api/types.go, but there's a circular dep. Fix it.
|
||||||
const (
|
const (
|
||||||
// ValidationErrorTypeNotFound is used to report failure to find a requested value
|
// ErrorType is used to report failure to find a requested value
|
||||||
// (e.g. looking up an ID).
|
// (e.g. looking up an ID).
|
||||||
ValidationErrorTypeNotFound ValidationErrorType = "FieldValueNotFound"
|
ErrorTypeNotFound ErrorType = "FieldValueNotFound"
|
||||||
// ValidationErrorTypeRequired is used to report required values that are not
|
// ErrorTypeRequired is used to report required values that are not
|
||||||
// provided (e.g. empty strings, null values, or empty arrays).
|
// provided (e.g. empty strings, null values, or empty arrays).
|
||||||
ValidationErrorTypeRequired ValidationErrorType = "FieldValueRequired"
|
ErrorTypeRequired ErrorType = "FieldValueRequired"
|
||||||
// ValidationErrorTypeDuplicate is used to report collisions of values that must be
|
// ErrorTypeDuplicate is used to report collisions of values that must be
|
||||||
// unique (e.g. unique IDs).
|
// unique (e.g. unique IDs).
|
||||||
ValidationErrorTypeDuplicate ValidationErrorType = "FieldValueDuplicate"
|
ErrorTypeDuplicate ErrorType = "FieldValueDuplicate"
|
||||||
// ValidationErrorTypeInvalid is used to report malformed values (e.g. failed regex
|
// ErrorTypeInvalid is used to report malformed values (e.g. failed regex
|
||||||
// match).
|
// match).
|
||||||
ValidationErrorTypeInvalid ValidationErrorType = "FieldValueInvalid"
|
ErrorTypeInvalid ErrorType = "FieldValueInvalid"
|
||||||
// ValidationErrorTypeNotSupported is used to report valid (as per formatting rules)
|
// ErrorTypeNotSupported is used to report valid (as per formatting rules)
|
||||||
// values that can not be handled (e.g. an enumerated string).
|
// values that can not be handled (e.g. an enumerated string).
|
||||||
ValidationErrorTypeNotSupported ValidationErrorType = "FieldValueNotSupported"
|
ErrorTypeNotSupported ErrorType = "FieldValueNotSupported"
|
||||||
// ValidationErrorTypeForbidden is used to report valid (as per formatting rules)
|
// ErrorTypeForbidden is used to report valid (as per formatting rules)
|
||||||
// values which would be accepted by some api instances, but which would invoke behavior
|
// values which would be accepted by some api instances, but which would invoke behavior
|
||||||
// not permitted by this api instance (such as due to stricter security policy).
|
// not permitted by this api instance (such as due to stricter security policy).
|
||||||
ValidationErrorTypeForbidden ValidationErrorType = "FieldValueForbidden"
|
ErrorTypeForbidden ErrorType = "FieldValueForbidden"
|
||||||
// ValidationErrorTypeTooLong is used to report that given value is too long.
|
// ErrorTypeTooLong is used to report that given value is too long.
|
||||||
ValidationErrorTypeTooLong ValidationErrorType = "FieldValueTooLong"
|
ErrorTypeTooLong ErrorType = "FieldValueTooLong"
|
||||||
)
|
)
|
||||||
|
|
||||||
// String converts a ValidationErrorType into its corresponding error message.
|
// String converts a ErrorType into its corresponding error message.
|
||||||
func (t ValidationErrorType) String() string {
|
func (t ErrorType) String() string {
|
||||||
switch t {
|
switch t {
|
||||||
case ValidationErrorTypeNotFound:
|
case ErrorTypeNotFound:
|
||||||
return "not found"
|
return "not found"
|
||||||
case ValidationErrorTypeRequired:
|
case ErrorTypeRequired:
|
||||||
return "required value"
|
return "required value"
|
||||||
case ValidationErrorTypeDuplicate:
|
case ErrorTypeDuplicate:
|
||||||
return "duplicate value"
|
return "duplicate value"
|
||||||
case ValidationErrorTypeInvalid:
|
case ErrorTypeInvalid:
|
||||||
return "invalid value"
|
return "invalid value"
|
||||||
case ValidationErrorTypeNotSupported:
|
case ErrorTypeNotSupported:
|
||||||
return "unsupported value"
|
return "unsupported value"
|
||||||
case ValidationErrorTypeForbidden:
|
case ErrorTypeForbidden:
|
||||||
return "forbidden"
|
return "forbidden"
|
||||||
case ValidationErrorTypeTooLong:
|
case ErrorTypeTooLong:
|
||||||
return "too long"
|
return "too long"
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("unrecognized validation type: %#v", t))
|
panic(fmt.Sprintf("unrecognized validation type: %#v", t))
|
||||||
@ -80,7 +80,7 @@ func (t ValidationErrorType) String() string {
|
|||||||
|
|
||||||
// ValidationError is an implementation of the 'error' interface, which represents an error of validation.
|
// ValidationError is an implementation of the 'error' interface, which represents an error of validation.
|
||||||
type ValidationError struct {
|
type ValidationError struct {
|
||||||
Type ValidationErrorType
|
Type ErrorType
|
||||||
Field string
|
Field string
|
||||||
BadValue interface{}
|
BadValue interface{}
|
||||||
Detail string
|
Detail string
|
||||||
@ -95,7 +95,7 @@ func (v *ValidationError) Error() string {
|
|||||||
func (v *ValidationError) ErrorBody() string {
|
func (v *ValidationError) ErrorBody() string {
|
||||||
var s string
|
var s string
|
||||||
switch v.Type {
|
switch v.Type {
|
||||||
case ValidationErrorTypeRequired, ValidationErrorTypeTooLong:
|
case ErrorTypeRequired, ErrorTypeTooLong:
|
||||||
s = spew.Sprintf("%s", v.Type)
|
s = spew.Sprintf("%s", v.Type)
|
||||||
default:
|
default:
|
||||||
s = spew.Sprintf("%s '%+v'", v.Type, v.BadValue)
|
s = spew.Sprintf("%s '%+v'", v.Type, v.BadValue)
|
||||||
@ -108,12 +108,12 @@ func (v *ValidationError) ErrorBody() string {
|
|||||||
|
|
||||||
// NewFieldRequired returns a *ValidationError indicating "value required"
|
// NewFieldRequired returns a *ValidationError indicating "value required"
|
||||||
func NewFieldRequired(field string) *ValidationError {
|
func NewFieldRequired(field string) *ValidationError {
|
||||||
return &ValidationError{ValidationErrorTypeRequired, field, "", ""}
|
return &ValidationError{ErrorTypeRequired, field, "", ""}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFieldInvalid returns a *ValidationError indicating "invalid value"
|
// NewFieldInvalid returns a *ValidationError indicating "invalid value"
|
||||||
func NewFieldInvalid(field string, value interface{}, detail string) *ValidationError {
|
func NewFieldInvalid(field string, value interface{}, detail string) *ValidationError {
|
||||||
return &ValidationError{ValidationErrorTypeInvalid, field, value, detail}
|
return &ValidationError{ErrorTypeInvalid, field, value, detail}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFieldValueNotSupported returns a *ValidationError indicating "unsupported value"
|
// NewFieldValueNotSupported returns a *ValidationError indicating "unsupported value"
|
||||||
@ -122,26 +122,26 @@ func NewFieldValueNotSupported(field string, value interface{}, validValues []st
|
|||||||
if validValues != nil && len(validValues) > 0 {
|
if validValues != nil && len(validValues) > 0 {
|
||||||
detail = "supported values: " + strings.Join(validValues, ", ")
|
detail = "supported values: " + strings.Join(validValues, ", ")
|
||||||
}
|
}
|
||||||
return &ValidationError{ValidationErrorTypeNotSupported, field, value, detail}
|
return &ValidationError{ErrorTypeNotSupported, field, value, detail}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFieldForbidden returns a *ValidationError indicating "forbidden"
|
// NewFieldForbidden returns a *ValidationError indicating "forbidden"
|
||||||
func NewFieldForbidden(field string, value interface{}) *ValidationError {
|
func NewFieldForbidden(field string, value interface{}) *ValidationError {
|
||||||
return &ValidationError{ValidationErrorTypeForbidden, field, value, ""}
|
return &ValidationError{ErrorTypeForbidden, field, value, ""}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFieldDuplicate returns a *ValidationError indicating "duplicate value"
|
// NewFieldDuplicate returns a *ValidationError indicating "duplicate value"
|
||||||
func NewFieldDuplicate(field string, value interface{}) *ValidationError {
|
func NewFieldDuplicate(field string, value interface{}) *ValidationError {
|
||||||
return &ValidationError{ValidationErrorTypeDuplicate, field, value, ""}
|
return &ValidationError{ErrorTypeDuplicate, field, value, ""}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFieldNotFound returns a *ValidationError indicating "value not found"
|
// NewFieldNotFound returns a *ValidationError indicating "value not found"
|
||||||
func NewFieldNotFound(field string, value interface{}) *ValidationError {
|
func NewFieldNotFound(field string, value interface{}) *ValidationError {
|
||||||
return &ValidationError{ValidationErrorTypeNotFound, field, value, ""}
|
return &ValidationError{ErrorTypeNotFound, field, value, ""}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFieldTooLong(field string, value interface{}, maxLength int) *ValidationError {
|
func NewFieldTooLong(field string, value interface{}, maxLength int) *ValidationError {
|
||||||
return &ValidationError{ValidationErrorTypeTooLong, field, value, fmt.Sprintf("must have at most %d characters", maxLength)}
|
return &ValidationError{ErrorTypeTooLong, field, value, fmt.Sprintf("must have at most %d characters", maxLength)}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ValidationErrorList []error
|
type ValidationErrorList []error
|
||||||
@ -173,8 +173,8 @@ func (list ValidationErrorList) PrefixIndex(index int) ValidationErrorList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewValidationErrorFieldPrefixMatcher returns an errors.Matcher that returns true
|
// NewValidationErrorFieldPrefixMatcher returns an errors.Matcher that returns true
|
||||||
// if the provided error is a ValidationError and has the provided ValidationErrorType.
|
// if the provided error is a ValidationError and has the provided ErrorType.
|
||||||
func NewValidationErrorTypeMatcher(t ValidationErrorType) utilerrors.Matcher {
|
func NewErrorTypeMatcher(t ErrorType) utilerrors.Matcher {
|
||||||
return func(err error) bool {
|
return func(err error) bool {
|
||||||
if e, ok := err.(*ValidationError); ok {
|
if e, ok := err.(*ValidationError); ok {
|
||||||
return e.Type == t
|
return e.Type == t
|
||||||
|
@ -24,27 +24,27 @@ import (
|
|||||||
func TestMakeFuncs(t *testing.T) {
|
func TestMakeFuncs(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
fn func() *ValidationError
|
fn func() *ValidationError
|
||||||
expected ValidationErrorType
|
expected ErrorType
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
func() *ValidationError { return NewFieldInvalid("f", "v", "d") },
|
func() *ValidationError { return NewFieldInvalid("f", "v", "d") },
|
||||||
ValidationErrorTypeInvalid,
|
ErrorTypeInvalid,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
func() *ValidationError { return NewFieldValueNotSupported("f", "v", nil) },
|
func() *ValidationError { return NewFieldValueNotSupported("f", "v", nil) },
|
||||||
ValidationErrorTypeNotSupported,
|
ErrorTypeNotSupported,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
func() *ValidationError { return NewFieldDuplicate("f", "v") },
|
func() *ValidationError { return NewFieldDuplicate("f", "v") },
|
||||||
ValidationErrorTypeDuplicate,
|
ErrorTypeDuplicate,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
func() *ValidationError { return NewFieldNotFound("f", "v") },
|
func() *ValidationError { return NewFieldNotFound("f", "v") },
|
||||||
ValidationErrorTypeNotFound,
|
ErrorTypeNotFound,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
func() *ValidationError { return NewFieldRequired("f") },
|
func() *ValidationError { return NewFieldRequired("f") },
|
||||||
ValidationErrorTypeRequired,
|
ErrorTypeRequired,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ func TestMakeFuncs(t *testing.T) {
|
|||||||
func TestValidationErrorUsefulMessage(t *testing.T) {
|
func TestValidationErrorUsefulMessage(t *testing.T) {
|
||||||
s := NewFieldInvalid("foo", "bar", "deet").Error()
|
s := NewFieldInvalid("foo", "bar", "deet").Error()
|
||||||
t.Logf("message: %v", s)
|
t.Logf("message: %v", s)
|
||||||
for _, part := range []string{"foo", "bar", "deet", ValidationErrorTypeInvalid.String()} {
|
for _, part := range []string{"foo", "bar", "deet", ErrorTypeInvalid.String()} {
|
||||||
if !strings.Contains(s, part) {
|
if !strings.Contains(s, part) {
|
||||||
t.Errorf("error message did not contain expected part '%v'", part)
|
t.Errorf("error message did not contain expected part '%v'", part)
|
||||||
}
|
}
|
||||||
@ -83,7 +83,7 @@ func TestValidationErrorUsefulMessage(t *testing.T) {
|
|||||||
).Error()
|
).Error()
|
||||||
t.Logf("message: %v", s)
|
t.Logf("message: %v", s)
|
||||||
for _, part := range []string{
|
for _, part := range []string{
|
||||||
"foo", ValidationErrorTypeInvalid.String(),
|
"foo", ErrorTypeInvalid.String(),
|
||||||
"Baz", "Qux", "Inner", "KV", "detail",
|
"Baz", "Qux", "Inner", "KV", "detail",
|
||||||
"1", "aoeu", "asdf", "Billy", "2",
|
"1", "aoeu", "asdf", "Billy", "2",
|
||||||
} {
|
} {
|
||||||
@ -99,10 +99,10 @@ func TestErrListFilter(t *testing.T) {
|
|||||||
NewFieldInvalid("field.test", "", ""),
|
NewFieldInvalid("field.test", "", ""),
|
||||||
NewFieldDuplicate("test", "value"),
|
NewFieldDuplicate("test", "value"),
|
||||||
}
|
}
|
||||||
if len(list.Filter(NewValidationErrorTypeMatcher(ValidationErrorTypeDuplicate))) != 2 {
|
if len(list.Filter(NewErrorTypeMatcher(ErrorTypeDuplicate))) != 2 {
|
||||||
t.Errorf("should not filter")
|
t.Errorf("should not filter")
|
||||||
}
|
}
|
||||||
if len(list.Filter(NewValidationErrorTypeMatcher(ValidationErrorTypeInvalid))) != 1 {
|
if len(list.Filter(NewErrorTypeMatcher(ErrorTypeInvalid))) != 1 {
|
||||||
t.Errorf("should filter")
|
t.Errorf("should filter")
|
||||||
}
|
}
|
||||||
if len(list.Filter(NewValidationErrorFieldPrefixMatcher("test"))) != 1 {
|
if len(list.Filter(NewValidationErrorFieldPrefixMatcher("test"))) != 1 {
|
||||||
@ -114,7 +114,7 @@ func TestErrListFilter(t *testing.T) {
|
|||||||
if len(list.Filter(NewValidationErrorFieldPrefixMatcher(""))) != 0 {
|
if len(list.Filter(NewValidationErrorFieldPrefixMatcher(""))) != 0 {
|
||||||
t.Errorf("should filter")
|
t.Errorf("should filter")
|
||||||
}
|
}
|
||||||
if len(list.Filter(NewValidationErrorFieldPrefixMatcher("field."), NewValidationErrorTypeMatcher(ValidationErrorTypeDuplicate))) != 1 {
|
if len(list.Filter(NewValidationErrorFieldPrefixMatcher("field."), NewErrorTypeMatcher(ErrorTypeDuplicate))) != 1 {
|
||||||
t.Errorf("should filter")
|
t.Errorf("should filter")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user