Unify validation logic for create and update paths
Ensure ObjectMeta is consistently validated on both create and update Make PortalIP uncleareable
This commit is contained in:
@@ -85,9 +85,10 @@ var _ error = &ValidationError{}
|
||||
|
||||
func (v *ValidationError) Error() string {
|
||||
var s string
|
||||
if v.Type == ValidationErrorTypeRequired && isEmpty(v.BadValue) {
|
||||
switch v.Type {
|
||||
case ValidationErrorTypeRequired:
|
||||
s = spew.Sprintf("%s: %s", v.Field, v.Type)
|
||||
} else {
|
||||
default:
|
||||
s = spew.Sprintf("%s: %s '%+v'", v.Field, v.Type, v.BadValue)
|
||||
}
|
||||
if len(v.Detail) != 0 {
|
||||
@@ -96,18 +97,8 @@ func (v *ValidationError) Error() string {
|
||||
return s
|
||||
}
|
||||
|
||||
func isEmpty(obj interface{}) bool {
|
||||
if obj == nil {
|
||||
return true
|
||||
}
|
||||
switch t := obj.(type) {
|
||||
case string:
|
||||
return len(t) == 0
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// NewFieldRequired returns a *ValidationError indicating "value required"
|
||||
// TODO: remove "value"
|
||||
func NewFieldRequired(field string, value interface{}) *ValidationError {
|
||||
return &ValidationError{ValidationErrorTypeRequired, field, value, ""}
|
||||
}
|
||||
|
@@ -71,7 +71,7 @@ func TestValidationErrorUsefulMessage(t *testing.T) {
|
||||
Inner interface{}
|
||||
KV map[string]int
|
||||
}
|
||||
s = NewFieldRequired(
|
||||
s = NewFieldInvalid(
|
||||
"foo",
|
||||
&complicated{
|
||||
Baz: 1,
|
||||
@@ -79,11 +79,12 @@ func TestValidationErrorUsefulMessage(t *testing.T) {
|
||||
Inner: &complicated{Qux: "asdf"},
|
||||
KV: map[string]int{"Billy": 2},
|
||||
},
|
||||
"detail",
|
||||
).Error()
|
||||
t.Logf("message: %v", s)
|
||||
for _, part := range []string{
|
||||
"foo", ValidationErrorTypeRequired.String(),
|
||||
"Baz", "Qux", "Inner", "KV",
|
||||
"foo", ValidationErrorTypeInvalid.String(),
|
||||
"Baz", "Qux", "Inner", "KV", "detail",
|
||||
"1", "aoeu", "asdf", "Billy", "2",
|
||||
} {
|
||||
if !strings.Contains(s, part) {
|
||||
|
Reference in New Issue
Block a user