From 9a98f950632145ebc15dbbb96a457ccdc495c3de Mon Sep 17 00:00:00 2001 From: ElvinChan Date: Mon, 7 Jan 2019 13:14:57 +0800 Subject: [PATCH] #17 Add support for `map[string]interface{}` like type in schema --- internal_test.go | 12 +++++++++--- validator.go | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/internal_test.go b/internal_test.go index 38dec01..2e42335 100644 --- a/internal_test.go +++ b/internal_test.go @@ -141,9 +141,10 @@ func TestSchemaTypes(t *testing.T) { var pe map[time.Time]string var pf map[*int]string type PU struct { - Unknown interface{} + Any interface{} } var pg PU + var ph map[string]interface{} tests := []struct { p interface{} panic bool @@ -196,8 +197,13 @@ func TestSchemaTypes(t *testing.T) { }, { p: &pg, - panic: true, - name: "Struct inner invalid type", + panic: false, + name: "Struct interface field type", + }, + { + p: &ph, + panic: false, + name: "Map interface value type", }, } for _, tt := range tests { diff --git a/validator.go b/validator.go index 2689003..883772f 100644 --- a/validator.go +++ b/validator.go @@ -78,7 +78,7 @@ func isValidSchema(t reflect.Type, inner bool, pres ...reflect.Type) bool { switch t.Kind() { case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, - reflect.Float32, reflect.Float64, reflect.String: + reflect.Float32, reflect.Float64, reflect.String, reflect.Interface: return true case reflect.Array, reflect.Slice: return isValidSchema(t.Elem(), inner, pres...)