diff --git a/apis/field_error.go b/apis/field_error.go index 59b281d6ee..c3bb68bf2d 100644 --- a/apis/field_error.go +++ b/apis/field_error.go @@ -312,13 +312,13 @@ func ErrDisallowedUpdateDeprecatedFields(fieldPaths ...string) *FieldError { } // ErrInvalidArrayValue constructs a FieldError for a repetetive `field` -// at `index` that has received an invalid string value. +// at `index` that has received an invalid value. func ErrInvalidArrayValue(value interface{}, field string, index int) *FieldError { return ErrInvalidValue(value, CurrentField).ViaFieldIndex(field, index) } // ErrInvalidValue constructs a FieldError for a field that has received an -// invalid string value. +// invalid value. func ErrInvalidValue(value interface{}, fieldPath string) *FieldError { return &FieldError{ Message: fmt.Sprintf("invalid value: %v", value), @@ -326,6 +326,15 @@ func ErrInvalidValue(value interface{}, fieldPath string) *FieldError { } } +// ErrGeneric constructs a FieldError to allow for the different error strings for the +// the different cases. +func ErrGeneric(diagnostic string, fieldPaths ...string) *FieldError { + return &FieldError{ + Message: diagnostic, + Paths: fieldPaths, + } +} + // ErrMissingOneOf is a variadic helper method for constructing a FieldError for // not having at least one field in a mutually exclusive field group. func ErrMissingOneOf(fieldPaths ...string) *FieldError { diff --git a/apis/field_error_test.go b/apis/field_error_test.go index 6185d87df4..69f457fc30 100644 --- a/apis/field_error_test.go +++ b/apis/field_error_test.go @@ -158,6 +158,11 @@ Body.`, err: ErrInvalidValue(5*time.Second, "bar"), prefixes: [][]string{{"baz"}}, want: `invalid value: 5s: baz.bar`, + }, { + name: "invalid value propagation", + err: ErrGeneric("this is a generic error", "foo", "bar"), + prefixes: [][]string{{"baz"}}, + want: `this is a generic error: baz.bar, baz.foo`, }, { name: "missing mutually exclusive fields", err: ErrMissingOneOf("foo", "bar"),