Update err handling

This commit is contained in:
cici37 2022-03-24 15:21:28 -07:00
parent 383eb99beb
commit c6d4b14730
2 changed files with 16 additions and 8 deletions

View File

@ -85,24 +85,32 @@ func ValidateCustomResource(fldPath *field.Path, customResource interface{}, val
value = err.Value
}
max := int64(-1)
if i, ok := err.Value.(int64); ok {
if i, ok := err.Valid.(int64); ok {
max = i
}
allErrs = append(allErrs, field.TooLongMaxLength(errPath, value, int(max)))
case openapierrors.MaxItemsFailCode:
max := int64(-1)
actual := int64(-1)
if i, ok := err.Value.(int64); ok {
actual = i
}
max := int64(-1)
if i, ok := err.Valid.(int64); ok {
max = i
}
allErrs = append(allErrs, field.TooMany(errPath, int(max), -1))
allErrs = append(allErrs, field.TooMany(errPath, int(actual), int(max)))
case openapierrors.TooManyPropertiesCode:
max := int64(-1)
actual := int64(-1)
if i, ok := err.Value.(int64); ok {
actual = i
}
max := int64(-1)
if i, ok := err.Valid.(int64); ok {
max = i
}
allErrs = append(allErrs, field.TooMany(errPath, -1, int(max)))
allErrs = append(allErrs, field.TooMany(errPath, int(actual), int(max)))
case openapierrors.InvalidTypeCode:
value := interface{}("")

View File

@ -546,7 +546,7 @@ func TestValidateCustomResource(t *testing.T) {
},
failingObjects: []failingObject{
{object: map[string]interface{}{"fieldX": map[string]interface{}{"a": true, "b": true, "c": true}}, expectErrs: []string{
`fieldX: Too many: must have at most 2 items`,
`fieldX: Too many: 3: must have at most 2 items`,
}},
},
},
@ -561,7 +561,7 @@ func TestValidateCustomResource(t *testing.T) {
},
failingObjects: []failingObject{
{object: map[string]interface{}{"fieldX": []interface{}{"a", "b", "c"}}, expectErrs: []string{
`fieldX: Too many: 3: has too many items`,
`fieldX: Too many: 3: must have at most 2 items`,
}},
},
},
@ -576,7 +576,7 @@ func TestValidateCustomResource(t *testing.T) {
},
failingObjects: []failingObject{
{object: map[string]interface{}{"fieldX": "abc"}, expectErrs: []string{
`fieldX: Too long: value is too long`,
`fieldX: Too long: may not be longer than 2`,
}},
},
},