From 9584e4ea5c39e375b4635724e918682497c030ed Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Fri, 29 May 2015 20:34:41 +0200 Subject: [PATCH] Fix for #310 --- binding/binding.go | 13 +++++++++++++ binding/validate_test.go | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/binding/binding.go b/binding/binding.go index eb9aa6d8..5ca6dc7b 100644 --- a/binding/binding.go +++ b/binding/binding.go @@ -6,6 +6,7 @@ package binding import ( "net/http" + "reflect" "gopkg.in/bluesuncorp/validator.v5" ) @@ -56,8 +57,20 @@ func ValidateField(f interface{}, tag string) error { } func Validate(obj interface{}) error { + if kindOfData(obj) != reflect.Struct { + return nil + } if err := validate.Struct(obj); err != nil { return error(err) } return nil } + +func kindOfData(data interface{}) reflect.Kind { + value := reflect.ValueOf(data) + valueType := value.Kind() + if valueType == reflect.Ptr { + valueType = value.Elem().Kind() + } + return valueType +} diff --git a/binding/validate_test.go b/binding/validate_test.go index ba0c18c1..f4854105 100644 --- a/binding/validate_test.go +++ b/binding/validate_test.go @@ -51,3 +51,19 @@ func TestValidateGoodObject(t *testing.T) { test := createStruct() assert.Nil(t, Validate(&test)) } + +type Object map[string]interface{} +type MyObjects []Object + +func TestValidateSlice(t *testing.T) { + var obj MyObjects + var obj2 Object + var nu = 10 + + assert.NoError(t, Validate(obj)) + assert.NoError(t, Validate(&obj)) + assert.NoError(t, Validate(obj2)) + assert.NoError(t, Validate(&obj2)) + assert.NoError(t, Validate(nu)) + assert.NoError(t, Validate(&nu)) +}