Merge branch 'issue-#6' into v1-development

pull/16/head
Dean Karn 10 years ago
commit 5131e5e189
  1. 6
      validator.go
  2. 34
      validator_test.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

@ -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)

Loading…
Cancel
Save