Skip to content

Commit d99bb37

Browse files
authored
polish attribute validate (#2511)
1 parent ac01691 commit d99bb37

File tree

2 files changed

+2
-10
lines changed

2 files changed

+2
-10
lines changed

pkg/attribute/attribute.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727

2828
const (
2929
errUnsupportedAttribute = "unsupported attribute %v"
30-
errUnexpectedType = `unexpected type on attribute %v. expect %s, received %[3]v(%[3]T)`
3130
)
3231

3332
var (
@@ -270,14 +269,7 @@ func (d Dictionary) Validate(attrs map[string]interface{}) error {
270269
}
271270
}
272271

273-
if desc.typ != unknownType && desc.typ != reflect.TypeOf(v) {
274-
// Allow implicit conversion from int to float to ease typing
275-
if !(desc.typ == floatType && reflect.TypeOf(v) == intType) {
276-
return fmt.Errorf(errUnexpectedType, k, desc.typ, v)
277-
}
278-
}
279-
280-
if desc.checker != nil {
272+
if v != nil && desc.checker != nil {
281273
if err := desc.checker(v); err != nil {
282274
return err
283275
}

pkg/attribute/attribute_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func TestDictionaryValidate(t *testing.T) {
133133
a.NoError(tb.Validate(map[string]interface{}{"a": 1}))
134134
a.EqualError(tb.Validate(map[string]interface{}{"a": -1}), "some error")
135135
a.EqualError(tb.Validate(map[string]interface{}{"_a": -1}), fmt.Sprintf(errUnsupportedAttribute, "_a"))
136-
a.EqualError(tb.Validate(map[string]interface{}{"a": 1.0}), fmt.Sprintf(errUnexpectedType, "a", "int", 1.))
136+
a.EqualError(tb.Validate(map[string]interface{}{"a": 1.0}), "attribute a must be of type int, but got float64")
137137
a.NoError(tb.Validate(map[string]interface{}{"b": float32(1.0)}))
138138
a.NoError(tb.Validate(map[string]interface{}{"b": 1}))
139139
}

0 commit comments

Comments
 (0)