mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 09:49:50 +00:00
s/ValidationError/Error/
This commit is contained in:
parent
2a127d089a
commit
d64d1fbb3d
@ -162,7 +162,7 @@ func NewConflict(kind, name string, err error) error {
|
|||||||
func NewInvalid(kind, name string, errs validation.ValidationErrorList) error {
|
func NewInvalid(kind, name string, errs validation.ValidationErrorList) error {
|
||||||
causes := make([]unversioned.StatusCause, 0, len(errs))
|
causes := make([]unversioned.StatusCause, 0, len(errs))
|
||||||
for i := range errs {
|
for i := range errs {
|
||||||
if err, ok := errs[i].(*validation.ValidationError); ok {
|
if err, ok := errs[i].(*validation.Error); ok {
|
||||||
causes = append(causes, unversioned.StatusCause{
|
causes = append(causes, unversioned.StatusCause{
|
||||||
Type: unversioned.CauseType(err.Type),
|
Type: unversioned.CauseType(err.Type),
|
||||||
Message: err.ErrorBody(),
|
Message: err.ErrorBody(),
|
||||||
|
@ -88,7 +88,7 @@ func TestErrorNew(t *testing.T) {
|
|||||||
|
|
||||||
func TestNewInvalid(t *testing.T) {
|
func TestNewInvalid(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
Err *validation.ValidationError
|
Err *validation.Error
|
||||||
Details *unversioned.StatusDetails
|
Details *unversioned.StatusDetails
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
@ -1497,7 +1497,7 @@ func ValidateNodeUpdate(node, oldNode *api.Node) validation.ValidationErrorList
|
|||||||
// Clear status
|
// Clear status
|
||||||
oldNode.Status = node.Status
|
oldNode.Status = node.Status
|
||||||
|
|
||||||
// TODO: Add a 'real' ValidationError type for this error and provide print actual diffs.
|
// TODO: Add a 'real' error type for this error and provide print actual diffs.
|
||||||
if !api.Semantic.DeepEqual(oldNode, node) {
|
if !api.Semantic.DeepEqual(oldNode, node) {
|
||||||
glog.V(4).Infof("Update failed validation %#v vs %#v", oldNode, node)
|
glog.V(4).Infof("Update failed validation %#v vs %#v", oldNode, node)
|
||||||
allErrs = append(allErrs, fmt.Errorf("update contains more than labels or capacity changes"))
|
allErrs = append(allErrs, fmt.Errorf("update contains more than labels or capacity changes"))
|
||||||
|
@ -36,7 +36,7 @@ import (
|
|||||||
|
|
||||||
func expectPrefix(t *testing.T, prefix string, errs validation.ValidationErrorList) {
|
func expectPrefix(t *testing.T, prefix string, errs validation.ValidationErrorList) {
|
||||||
for i := range errs {
|
for i := range errs {
|
||||||
if f, p := errs[i].(*validation.ValidationError).Field, prefix; !strings.HasPrefix(f, p) {
|
if f, p := errs[i].(*validation.Error).Field, prefix; !strings.HasPrefix(f, p) {
|
||||||
t.Errorf("expected prefix '%s' for field '%s' (%v)", p, f, errs[i])
|
t.Errorf("expected prefix '%s' for field '%s' (%v)", p, f, errs[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -150,7 +150,7 @@ func TestValidateLabels(t *testing.T) {
|
|||||||
if len(errs) != 1 {
|
if len(errs) != 1 {
|
||||||
t.Errorf("case[%d] expected failure", i)
|
t.Errorf("case[%d] expected failure", i)
|
||||||
} else {
|
} else {
|
||||||
detail := errs[0].(*validation.ValidationError).Detail
|
detail := errs[0].(*validation.Error).Detail
|
||||||
if detail != qualifiedNameErrorMsg {
|
if detail != qualifiedNameErrorMsg {
|
||||||
t.Errorf("error detail %s should be equal %s", detail, qualifiedNameErrorMsg)
|
t.Errorf("error detail %s should be equal %s", detail, qualifiedNameErrorMsg)
|
||||||
}
|
}
|
||||||
@ -168,7 +168,7 @@ func TestValidateLabels(t *testing.T) {
|
|||||||
if len(errs) != 1 {
|
if len(errs) != 1 {
|
||||||
t.Errorf("case[%d] expected failure", i)
|
t.Errorf("case[%d] expected failure", i)
|
||||||
} else {
|
} else {
|
||||||
detail := errs[0].(*validation.ValidationError).Detail
|
detail := errs[0].(*validation.Error).Detail
|
||||||
if detail != labelValueErrorMsg {
|
if detail != labelValueErrorMsg {
|
||||||
t.Errorf("error detail %s should be equal %s", detail, labelValueErrorMsg)
|
t.Errorf("error detail %s should be equal %s", detail, labelValueErrorMsg)
|
||||||
}
|
}
|
||||||
@ -215,7 +215,7 @@ func TestValidateAnnotations(t *testing.T) {
|
|||||||
if len(errs) != 1 {
|
if len(errs) != 1 {
|
||||||
t.Errorf("case[%d] expected failure", i)
|
t.Errorf("case[%d] expected failure", i)
|
||||||
}
|
}
|
||||||
detail := errs[0].(*validation.ValidationError).Detail
|
detail := errs[0].(*validation.Error).Detail
|
||||||
if detail != qualifiedNameErrorMsg {
|
if detail != qualifiedNameErrorMsg {
|
||||||
t.Errorf("error detail %s should be equal %s", detail, qualifiedNameErrorMsg)
|
t.Errorf("error detail %s should be equal %s", detail, qualifiedNameErrorMsg)
|
||||||
}
|
}
|
||||||
@ -568,13 +568,13 @@ func TestValidateVolumes(t *testing.T) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for i := range errs {
|
for i := range errs {
|
||||||
if errs[i].(*validation.ValidationError).Type != v.T {
|
if errs[i].(*validation.Error).Type != v.T {
|
||||||
t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
|
t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
|
||||||
}
|
}
|
||||||
if errs[i].(*validation.ValidationError).Field != v.F {
|
if errs[i].(*validation.Error).Field != v.F {
|
||||||
t.Errorf("%s: expected errors to have field %s: %v", k, v.F, errs[i])
|
t.Errorf("%s: expected errors to have field %s: %v", k, v.F, errs[i])
|
||||||
}
|
}
|
||||||
detail := errs[i].(*validation.ValidationError).Detail
|
detail := errs[i].(*validation.Error).Detail
|
||||||
if detail != v.D {
|
if detail != v.D {
|
||||||
t.Errorf("%s: expected error detail \"%s\", got \"%s\"", k, v.D, detail)
|
t.Errorf("%s: expected error detail \"%s\", got \"%s\"", k, v.D, detail)
|
||||||
}
|
}
|
||||||
@ -627,13 +627,13 @@ func TestValidatePorts(t *testing.T) {
|
|||||||
t.Errorf("expected failure for %s", k)
|
t.Errorf("expected failure for %s", k)
|
||||||
}
|
}
|
||||||
for i := range errs {
|
for i := range errs {
|
||||||
if errs[i].(*validation.ValidationError).Type != v.T {
|
if errs[i].(*validation.Error).Type != v.T {
|
||||||
t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
|
t.Errorf("%s: expected errors to have type %s: %v", k, v.T, errs[i])
|
||||||
}
|
}
|
||||||
if errs[i].(*validation.ValidationError).Field != v.F {
|
if errs[i].(*validation.Error).Field != v.F {
|
||||||
t.Errorf("%s: expected errors to have field %s: %v", k, v.F, errs[i])
|
t.Errorf("%s: expected errors to have field %s: %v", k, v.F, errs[i])
|
||||||
}
|
}
|
||||||
detail := errs[i].(*validation.ValidationError).Detail
|
detail := errs[i].(*validation.Error).Detail
|
||||||
if detail != v.D {
|
if detail != v.D {
|
||||||
t.Errorf("%s: expected error detail either empty or %s, got %s", k, v.D, detail)
|
t.Errorf("%s: expected error detail either empty or %s, got %s", k, v.D, detail)
|
||||||
}
|
}
|
||||||
@ -772,7 +772,7 @@ func TestValidateEnv(t *testing.T) {
|
|||||||
t.Errorf("expected failure for %s", tc.name)
|
t.Errorf("expected failure for %s", tc.name)
|
||||||
} else {
|
} else {
|
||||||
for i := range errs {
|
for i := range errs {
|
||||||
str := errs[i].(*validation.ValidationError).Error()
|
str := errs[i].(*validation.Error).Error()
|
||||||
if str != "" && str != tc.expectedError {
|
if str != "" && str != tc.expectedError {
|
||||||
t.Errorf("%s: expected error detail either empty or %s, got %s", tc.name, tc.expectedError, str)
|
t.Errorf("%s: expected error detail either empty or %s, got %s", tc.name, tc.expectedError, str)
|
||||||
}
|
}
|
||||||
@ -2560,7 +2560,7 @@ func TestValidateReplicationController(t *testing.T) {
|
|||||||
t.Errorf("expected failure for %s", k)
|
t.Errorf("expected failure for %s", k)
|
||||||
}
|
}
|
||||||
for i := range errs {
|
for i := range errs {
|
||||||
field := errs[i].(*validation.ValidationError).Field
|
field := errs[i].(*validation.Error).Field
|
||||||
if !strings.HasPrefix(field, "spec.template.") &&
|
if !strings.HasPrefix(field, "spec.template.") &&
|
||||||
field != "metadata.name" &&
|
field != "metadata.name" &&
|
||||||
field != "metadata.namespace" &&
|
field != "metadata.namespace" &&
|
||||||
@ -2676,7 +2676,7 @@ func TestValidateNode(t *testing.T) {
|
|||||||
t.Errorf("expected failure for %s", k)
|
t.Errorf("expected failure for %s", k)
|
||||||
}
|
}
|
||||||
for i := range errs {
|
for i := range errs {
|
||||||
field := errs[i].(*validation.ValidationError).Field
|
field := errs[i].(*validation.Error).Field
|
||||||
expectedFields := map[string]bool{
|
expectedFields := map[string]bool{
|
||||||
"metadata.name": true,
|
"metadata.name": true,
|
||||||
"metadata.labels": true,
|
"metadata.labels": true,
|
||||||
@ -3008,7 +3008,7 @@ func TestValidateResourceNames(t *testing.T) {
|
|||||||
} else if len(err) == 0 && !item.success {
|
} else if len(err) == 0 && !item.success {
|
||||||
t.Errorf("expected failure for input %q", item.input)
|
t.Errorf("expected failure for input %q", item.input)
|
||||||
for i := range err {
|
for i := range err {
|
||||||
detail := err[i].(*validation.ValidationError).Detail
|
detail := err[i].(*validation.Error).Detail
|
||||||
if detail != "" && detail != qualifiedNameErrorMsg {
|
if detail != "" && detail != qualifiedNameErrorMsg {
|
||||||
t.Errorf("%d: expected error detail either empty or %s, got %s", k, qualifiedNameErrorMsg, detail)
|
t.Errorf("%d: expected error detail either empty or %s, got %s", k, qualifiedNameErrorMsg, detail)
|
||||||
}
|
}
|
||||||
@ -3224,7 +3224,7 @@ func TestValidateLimitRange(t *testing.T) {
|
|||||||
t.Errorf("expected failure for %s", k)
|
t.Errorf("expected failure for %s", k)
|
||||||
}
|
}
|
||||||
for i := range errs {
|
for i := range errs {
|
||||||
detail := errs[i].(*validation.ValidationError).Detail
|
detail := errs[i].(*validation.Error).Detail
|
||||||
if detail != v.D {
|
if detail != v.D {
|
||||||
t.Errorf("%s: expected error detail either empty or %s, got %s", k, v.D, detail)
|
t.Errorf("%s: expected error detail either empty or %s, got %s", k, v.D, detail)
|
||||||
}
|
}
|
||||||
@ -3329,8 +3329,8 @@ func TestValidateResourceQuota(t *testing.T) {
|
|||||||
t.Errorf("expected failure for %s", k)
|
t.Errorf("expected failure for %s", k)
|
||||||
}
|
}
|
||||||
for i := range errs {
|
for i := range errs {
|
||||||
field := errs[i].(*validation.ValidationError).Field
|
field := errs[i].(*validation.Error).Field
|
||||||
detail := errs[i].(*validation.ValidationError).Detail
|
detail := errs[i].(*validation.Error).Detail
|
||||||
if field != "metadata.name" && field != "metadata.namespace" && !api.IsStandardResourceName(field) {
|
if field != "metadata.name" && field != "metadata.namespace" && !api.IsStandardResourceName(field) {
|
||||||
t.Errorf("%s: missing prefix for: %v", k, field)
|
t.Errorf("%s: missing prefix for: %v", k, field)
|
||||||
}
|
}
|
||||||
@ -3937,7 +3937,7 @@ func TestValidateEndpoints(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range errorCases {
|
for k, v := range errorCases {
|
||||||
if errs := ValidateEndpoints(&v.endpoints); len(errs) == 0 || errs[0].(*validation.ValidationError).Type != v.errorType || !strings.Contains(errs[0].(*validation.ValidationError).Detail, v.errorDetail) {
|
if errs := ValidateEndpoints(&v.endpoints); len(errs) == 0 || errs[0].(*validation.Error).Type != v.errorType || !strings.Contains(errs[0].(*validation.Error).Detail, v.errorDetail) {
|
||||||
t.Errorf("Expected error type %s with detail %s for %s, got %v", v.errorType, v.errorDetail, k, errs)
|
t.Errorf("Expected error type %s with detail %s for %s, got %v", v.errorType, v.errorDetail, k, errs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -4017,7 +4017,7 @@ func TestValidateSecurityContext(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for k, v := range errorCases {
|
for k, v := range errorCases {
|
||||||
if errs := ValidateSecurityContext(v.sc); len(errs) == 0 || errs[0].(*validation.ValidationError).Type != v.errorType || errs[0].(*validation.ValidationError).Detail != v.errorDetail {
|
if errs := ValidateSecurityContext(v.sc); len(errs) == 0 || errs[0].(*validation.Error).Type != v.errorType || errs[0].(*validation.Error).Detail != v.errorDetail {
|
||||||
t.Errorf("Expected error type %s with detail %s for %s, got %v", v.errorType, v.errorDetail, k, errs)
|
t.Errorf("Expected error type %s with detail %s for %s, got %v", v.errorType, v.errorDetail, k, errs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -675,7 +675,7 @@ func TestValidateDaemonSet(t *testing.T) {
|
|||||||
t.Errorf("expected failure for %s", k)
|
t.Errorf("expected failure for %s", k)
|
||||||
}
|
}
|
||||||
for i := range errs {
|
for i := range errs {
|
||||||
field := errs[i].(*validation.ValidationError).Field
|
field := errs[i].(*validation.Error).Field
|
||||||
if !strings.HasPrefix(field, "spec.template.") &&
|
if !strings.HasPrefix(field, "spec.template.") &&
|
||||||
field != "metadata.name" &&
|
field != "metadata.name" &&
|
||||||
field != "metadata.namespace" &&
|
field != "metadata.namespace" &&
|
||||||
@ -918,7 +918,7 @@ func TestValidateJob(t *testing.T) {
|
|||||||
t.Errorf("expected failure for %s", k)
|
t.Errorf("expected failure for %s", k)
|
||||||
} else {
|
} else {
|
||||||
s := strings.Split(k, ":")
|
s := strings.Split(k, ":")
|
||||||
err := errs[0].(*validation.ValidationError)
|
err := errs[0].(*validation.Error)
|
||||||
if err.Field != s[0] || !strings.Contains(err.Error(), s[1]) {
|
if err.Field != s[0] || !strings.Contains(err.Error(), s[1]) {
|
||||||
t.Errorf("unexpected error: %v, expected: %s", errs[0], k)
|
t.Errorf("unexpected error: %v, expected: %s", errs[0], k)
|
||||||
}
|
}
|
||||||
@ -1019,7 +1019,7 @@ func TestValidateIngress(t *testing.T) {
|
|||||||
t.Errorf("expected failure for %s", k)
|
t.Errorf("expected failure for %s", k)
|
||||||
} else {
|
} else {
|
||||||
s := strings.Split(k, ":")
|
s := strings.Split(k, ":")
|
||||||
err := errs[0].(*validation.ValidationError)
|
err := errs[0].(*validation.Error)
|
||||||
if err.Field != s[0] || !strings.Contains(err.Error(), s[1]) {
|
if err.Field != s[0] || !strings.Contains(err.Error(), s[1]) {
|
||||||
t.Errorf("unexpected error: %v, expected: %s", errs[0], k)
|
t.Errorf("unexpected error: %v, expected: %s", errs[0], k)
|
||||||
}
|
}
|
||||||
@ -1111,7 +1111,7 @@ func TestValidateIngressStatusUpdate(t *testing.T) {
|
|||||||
t.Errorf("expected failure for %s", k)
|
t.Errorf("expected failure for %s", k)
|
||||||
} else {
|
} else {
|
||||||
s := strings.Split(k, ":")
|
s := strings.Split(k, ":")
|
||||||
err := errs[0].(*validation.ValidationError)
|
err := errs[0].(*validation.Error)
|
||||||
if err.Field != s[0] || !strings.Contains(err.Error(), s[1]) {
|
if err.Field != s[0] || !strings.Contains(err.Error(), s[1]) {
|
||||||
t.Errorf("unexpected error: %v, expected: %s", errs[0], k)
|
t.Errorf("unexpected error: %v, expected: %s", errs[0], k)
|
||||||
}
|
}
|
||||||
|
@ -78,21 +78,21 @@ func (t ErrorType) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidationError is an implementation of the 'error' interface, which represents an error of validation.
|
// Error is an implementation of the 'error' interface, which represents an error of validation.
|
||||||
type ValidationError struct {
|
type Error struct {
|
||||||
Type ErrorType
|
Type ErrorType
|
||||||
Field string
|
Field string
|
||||||
BadValue interface{}
|
BadValue interface{}
|
||||||
Detail string
|
Detail string
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ error = &ValidationError{}
|
var _ error = &Error{}
|
||||||
|
|
||||||
func (v *ValidationError) Error() string {
|
func (v *Error) Error() string {
|
||||||
return fmt.Sprintf("%s: %s", v.Field, v.ErrorBody())
|
return fmt.Sprintf("%s: %s", v.Field, v.ErrorBody())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *ValidationError) ErrorBody() string {
|
func (v *Error) ErrorBody() string {
|
||||||
var s string
|
var s string
|
||||||
switch v.Type {
|
switch v.Type {
|
||||||
case ErrorTypeRequired, ErrorTypeTooLong:
|
case ErrorTypeRequired, ErrorTypeTooLong:
|
||||||
@ -106,51 +106,51 @@ func (v *ValidationError) ErrorBody() string {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFieldRequired returns a *ValidationError indicating "value required"
|
// NewFieldRequired returns a *Error indicating "value required"
|
||||||
func NewFieldRequired(field string) *ValidationError {
|
func NewFieldRequired(field string) *Error {
|
||||||
return &ValidationError{ErrorTypeRequired, field, "", ""}
|
return &Error{ErrorTypeRequired, field, "", ""}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFieldInvalid returns a *ValidationError indicating "invalid value"
|
// NewFieldInvalid returns a *Error indicating "invalid value"
|
||||||
func NewFieldInvalid(field string, value interface{}, detail string) *ValidationError {
|
func NewFieldInvalid(field string, value interface{}, detail string) *Error {
|
||||||
return &ValidationError{ErrorTypeInvalid, field, value, detail}
|
return &Error{ErrorTypeInvalid, field, value, detail}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFieldValueNotSupported returns a *ValidationError indicating "unsupported value"
|
// NewFieldValueNotSupported returns a *Error indicating "unsupported value"
|
||||||
func NewFieldValueNotSupported(field string, value interface{}, validValues []string) *ValidationError {
|
func NewFieldValueNotSupported(field string, value interface{}, validValues []string) *Error {
|
||||||
detail := ""
|
detail := ""
|
||||||
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{ErrorTypeNotSupported, field, value, detail}
|
return &Error{ErrorTypeNotSupported, field, value, detail}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFieldForbidden returns a *ValidationError indicating "forbidden"
|
// NewFieldForbidden returns a *Error indicating "forbidden"
|
||||||
func NewFieldForbidden(field string, value interface{}) *ValidationError {
|
func NewFieldForbidden(field string, value interface{}) *Error {
|
||||||
return &ValidationError{ErrorTypeForbidden, field, value, ""}
|
return &Error{ErrorTypeForbidden, field, value, ""}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFieldDuplicate returns a *ValidationError indicating "duplicate value"
|
// NewFieldDuplicate returns a *Error indicating "duplicate value"
|
||||||
func NewFieldDuplicate(field string, value interface{}) *ValidationError {
|
func NewFieldDuplicate(field string, value interface{}) *Error {
|
||||||
return &ValidationError{ErrorTypeDuplicate, field, value, ""}
|
return &Error{ErrorTypeDuplicate, field, value, ""}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFieldNotFound returns a *ValidationError indicating "value not found"
|
// NewFieldNotFound returns a *Error indicating "value not found"
|
||||||
func NewFieldNotFound(field string, value interface{}) *ValidationError {
|
func NewFieldNotFound(field string, value interface{}) *Error {
|
||||||
return &ValidationError{ErrorTypeNotFound, field, value, ""}
|
return &Error{ErrorTypeNotFound, field, value, ""}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFieldTooLong(field string, value interface{}, maxLength int) *ValidationError {
|
func NewFieldTooLong(field string, value interface{}, maxLength int) *Error {
|
||||||
return &ValidationError{ErrorTypeTooLong, field, value, fmt.Sprintf("must have at most %d characters", maxLength)}
|
return &Error{ErrorTypeTooLong, field, value, fmt.Sprintf("must have at most %d characters", maxLength)}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ValidationErrorList []error
|
type ValidationErrorList []error
|
||||||
|
|
||||||
// Prefix adds a prefix to the Field of every ValidationError in the list.
|
// Prefix adds a prefix to the Field of every Error in the list.
|
||||||
// Returns the list for convenience.
|
// Returns the list for convenience.
|
||||||
func (list ValidationErrorList) Prefix(prefix string) ValidationErrorList {
|
func (list ValidationErrorList) Prefix(prefix string) ValidationErrorList {
|
||||||
for i := range list {
|
for i := range list {
|
||||||
if err, ok := list[i].(*ValidationError); ok {
|
if err, ok := list[i].(*Error); ok {
|
||||||
if strings.HasPrefix(err.Field, "[") {
|
if strings.HasPrefix(err.Field, "[") {
|
||||||
err.Field = prefix + err.Field
|
err.Field = prefix + err.Field
|
||||||
} else if len(err.Field) != 0 {
|
} else if len(err.Field) != 0 {
|
||||||
@ -160,23 +160,23 @@ func (list ValidationErrorList) Prefix(prefix string) ValidationErrorList {
|
|||||||
}
|
}
|
||||||
list[i] = err
|
list[i] = err
|
||||||
} else {
|
} else {
|
||||||
panic(fmt.Sprintf("Programmer error: ValidationErrorList holds non-ValidationError: %#v", list[i]))
|
panic(fmt.Sprintf("Programmer error: ValidationErrorList holds non-Error: %#v", list[i]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrefixIndex adds an index to the Field of every ValidationError in the list.
|
// PrefixIndex adds an index to the Field of every Error in the list.
|
||||||
// Returns the list for convenience.
|
// Returns the list for convenience.
|
||||||
func (list ValidationErrorList) PrefixIndex(index int) ValidationErrorList {
|
func (list ValidationErrorList) PrefixIndex(index int) ValidationErrorList {
|
||||||
return list.Prefix(fmt.Sprintf("[%d]", index))
|
return list.Prefix(fmt.Sprintf("[%d]", index))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 ErrorType.
|
// if the provided error is a Error and has the provided ErrorType.
|
||||||
func NewErrorTypeMatcher(t ErrorType) 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.(*Error); ok {
|
||||||
return e.Type == t
|
return e.Type == t
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@ -184,11 +184,11 @@ func NewErrorTypeMatcher(t ErrorType) utilerrors.Matcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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 a field with the provided
|
// if the provided error is a Error and has a field with the provided
|
||||||
// prefix.
|
// prefix.
|
||||||
func NewValidationErrorFieldPrefixMatcher(prefix string) utilerrors.Matcher {
|
func NewValidationErrorFieldPrefixMatcher(prefix string) utilerrors.Matcher {
|
||||||
return func(err error) bool {
|
return func(err error) bool {
|
||||||
if e, ok := err.(*ValidationError); ok {
|
if e, ok := err.(*Error); ok {
|
||||||
return strings.HasPrefix(e.Field, prefix)
|
return strings.HasPrefix(e.Field, prefix)
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
@ -23,27 +23,27 @@ import (
|
|||||||
|
|
||||||
func TestMakeFuncs(t *testing.T) {
|
func TestMakeFuncs(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
fn func() *ValidationError
|
fn func() *Error
|
||||||
expected ErrorType
|
expected ErrorType
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
func() *ValidationError { return NewFieldInvalid("f", "v", "d") },
|
func() *Error { return NewFieldInvalid("f", "v", "d") },
|
||||||
ErrorTypeInvalid,
|
ErrorTypeInvalid,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
func() *ValidationError { return NewFieldValueNotSupported("f", "v", nil) },
|
func() *Error { return NewFieldValueNotSupported("f", "v", nil) },
|
||||||
ErrorTypeNotSupported,
|
ErrorTypeNotSupported,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
func() *ValidationError { return NewFieldDuplicate("f", "v") },
|
func() *Error { return NewFieldDuplicate("f", "v") },
|
||||||
ErrorTypeDuplicate,
|
ErrorTypeDuplicate,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
func() *ValidationError { return NewFieldNotFound("f", "v") },
|
func() *Error { return NewFieldNotFound("f", "v") },
|
||||||
ErrorTypeNotFound,
|
ErrorTypeNotFound,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
func() *ValidationError { return NewFieldRequired("f") },
|
func() *Error { return NewFieldRequired("f") },
|
||||||
ErrorTypeRequired,
|
ErrorTypeRequired,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -56,7 +56,7 @@ func TestMakeFuncs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidationErrorUsefulMessage(t *testing.T) {
|
func TestErrorUsefulMessage(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", ErrorTypeInvalid.String()} {
|
for _, part := range []string{"foo", "bar", "deet", ErrorTypeInvalid.String()} {
|
||||||
@ -121,7 +121,7 @@ func TestErrListFilter(t *testing.T) {
|
|||||||
|
|
||||||
func TestErrListPrefix(t *testing.T) {
|
func TestErrListPrefix(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
Err *ValidationError
|
Err *Error
|
||||||
Expected string
|
Expected string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -143,7 +143,7 @@ func TestErrListPrefix(t *testing.T) {
|
|||||||
if prefix == nil || len(prefix) != len(errList) {
|
if prefix == nil || len(prefix) != len(errList) {
|
||||||
t.Errorf("Prefix should return self")
|
t.Errorf("Prefix should return self")
|
||||||
}
|
}
|
||||||
if e, a := testCase.Expected, errList[0].(*ValidationError).Field; e != a {
|
if e, a := testCase.Expected, errList[0].(*Error).Field; e != a {
|
||||||
t.Errorf("expected %s, got %s", e, a)
|
t.Errorf("expected %s, got %s", e, a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -151,7 +151,7 @@ func TestErrListPrefix(t *testing.T) {
|
|||||||
|
|
||||||
func TestErrListPrefixIndex(t *testing.T) {
|
func TestErrListPrefixIndex(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
Err *ValidationError
|
Err *Error
|
||||||
Expected string
|
Expected string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -173,7 +173,7 @@ func TestErrListPrefixIndex(t *testing.T) {
|
|||||||
if prefix == nil || len(prefix) != len(errList) {
|
if prefix == nil || len(prefix) != len(errList) {
|
||||||
t.Errorf("PrefixIndex should return self")
|
t.Errorf("PrefixIndex should return self")
|
||||||
}
|
}
|
||||||
if e, a := testCase.Expected, errList[0].(*ValidationError).Field; e != a {
|
if e, a := testCase.Expected, errList[0].(*Error).Field; e != a {
|
||||||
t.Errorf("expected %s, got %s", e, a)
|
t.Errorf("expected %s, got %s", e, a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user