From 2334f777b899240066a3cea78316c88c80365fcb Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Sat, 14 Feb 2015 13:31:44 -0500 Subject: [PATCH] issue-#6 add interface support + Test --- validator.go | 6 +++--- validator_test.go | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/validator.go b/validator.go index 7201958..26bfcf2 100644 --- a/validator.go +++ b/validator.go @@ -178,7 +178,7 @@ func (v *Validator) ValidateStruct(s interface{}) *StructValidationErrors { return v.ValidateStruct(structValue.Elem().Interface()) } - if structValue.Kind() != reflect.Struct { + if structValue.Kind() != reflect.Struct && structValue.Kind() != reflect.Interface { panic("interface passed for validation is not a struct") } @@ -200,13 +200,13 @@ func (v *Validator) ValidateStruct(s interface{}) *StructValidationErrors { } // if no validation and not a struct (which may containt fields for validation) - if tag == "" && valueField.Kind() != reflect.Struct { + if tag == "" && valueField.Kind() != reflect.Struct && valueField.Kind() != reflect.Interface { continue } switch valueField.Kind() { - case reflect.Struct: + case reflect.Struct, reflect.Interface: if !unicode.IsUpper(rune(typeField.Name[0])) { continue diff --git a/validator_test.go b/validator_test.go index e3a1074..6731db2 100644 --- a/validator_test.go +++ b/validator_test.go @@ -8,10 +8,26 @@ import ( . "gopkg.in/check.v1" ) +type I interface { + Foo() string +} + +type Impl struct { + F string `validate:"len=3"` +} + +func (i *Impl) Foo() string { + return i.F +} + type SubTest struct { Test string `validate:"required"` } +type TestInterface struct { + Iface I +} + type TestString struct { Required string `validate:"required"` Len string `validate:"len=10"` @@ -24,6 +40,7 @@ type TestString struct { Anonymous struct { A string `validate:"required"` } + Iface I } type TestInt32 struct { @@ -116,6 +133,9 @@ func (ms *MySuite) TestFlattening(c *C) { }{ A: "1", }, + Iface: &Impl{ + F: "123", + }, } err1 := validator.ValidateStruct(tSuccess).Flatten() @@ -136,6 +156,9 @@ func (ms *MySuite) TestFlattening(c *C) { }{ A: "", }, + Iface: &Impl{ + F: "12", + }, } err2 := validator.ValidateStruct(tFail).Flatten() @@ -151,6 +174,9 @@ func (ms *MySuite) TestFlattening(c *C) { // Assert Anonymous Struct Field AssertMapFieldError(err2, "Anonymous.A", "required", c) + + // Assert Interface Field + AssertMapFieldError(err2, "Iface.F", "len", c) } func (ms *MySuite) TestStructStringValidation(c *C) { @@ -173,6 +199,9 @@ func (ms *MySuite) TestStructStringValidation(c *C) { }{ A: "1", }, + Iface: &Impl{ + F: "123", + }, } err := validator.ValidateStruct(tSuccess) @@ -193,6 +222,9 @@ func (ms *MySuite) TestStructStringValidation(c *C) { }{ A: "", }, + Iface: &Impl{ + F: "12", + }, } err = validator.ValidateStruct(tFail) @@ -201,7 +233,7 @@ func (ms *MySuite) TestStructStringValidation(c *C) { c.Assert(err, NotNil) c.Assert(err.Struct, Equals, "TestString") c.Assert(len(err.Errors), Equals, 6) - c.Assert(len(err.StructErrors), Equals, 2) + c.Assert(len(err.StructErrors), Equals, 3) // Assert Fields AssertFieldError(err, "Required", "required", c)