Skip to content

Commit 5131e5e

Browse files
Dean KarnDean Karn
authored andcommitted
Merge branch 'issue-#6' into v1-development
2 parents 8f9951b + 2334f77 commit 5131e5e

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

validator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (v *Validator) ValidateStruct(s interface{}) *StructValidationErrors {
178178
return v.ValidateStruct(structValue.Elem().Interface())
179179
}
180180

181-
if structValue.Kind() != reflect.Struct {
181+
if structValue.Kind() != reflect.Struct && structValue.Kind() != reflect.Interface {
182182
panic("interface passed for validation is not a struct")
183183
}
184184

@@ -200,13 +200,13 @@ func (v *Validator) ValidateStruct(s interface{}) *StructValidationErrors {
200200
}
201201

202202
// if no validation and not a struct (which may containt fields for validation)
203-
if tag == "" && valueField.Kind() != reflect.Struct {
203+
if tag == "" && valueField.Kind() != reflect.Struct && valueField.Kind() != reflect.Interface {
204204
continue
205205
}
206206

207207
switch valueField.Kind() {
208208

209-
case reflect.Struct:
209+
case reflect.Struct, reflect.Interface:
210210

211211
if !unicode.IsUpper(rune(typeField.Name[0])) {
212212
continue

validator_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,26 @@ import (
88
. "gopkg.in/check.v1"
99
)
1010

11+
type I interface {
12+
Foo() string
13+
}
14+
15+
type Impl struct {
16+
F string `validate:"len=3"`
17+
}
18+
19+
func (i *Impl) Foo() string {
20+
return i.F
21+
}
22+
1123
type SubTest struct {
1224
Test string `validate:"required"`
1325
}
1426

27+
type TestInterface struct {
28+
Iface I
29+
}
30+
1531
type TestString struct {
1632
Required string `validate:"required"`
1733
Len string `validate:"len=10"`
@@ -24,6 +40,7 @@ type TestString struct {
2440
Anonymous struct {
2541
A string `validate:"required"`
2642
}
43+
Iface I
2744
}
2845

2946
type TestInt32 struct {
@@ -116,6 +133,9 @@ func (ms *MySuite) TestFlattening(c *C) {
116133
}{
117134
A: "1",
118135
},
136+
Iface: &Impl{
137+
F: "123",
138+
},
119139
}
120140

121141
err1 := validator.ValidateStruct(tSuccess).Flatten()
@@ -136,6 +156,9 @@ func (ms *MySuite) TestFlattening(c *C) {
136156
}{
137157
A: "",
138158
},
159+
Iface: &Impl{
160+
F: "12",
161+
},
139162
}
140163

141164
err2 := validator.ValidateStruct(tFail).Flatten()
@@ -151,6 +174,9 @@ func (ms *MySuite) TestFlattening(c *C) {
151174

152175
// Assert Anonymous Struct Field
153176
AssertMapFieldError(err2, "Anonymous.A", "required", c)
177+
178+
// Assert Interface Field
179+
AssertMapFieldError(err2, "Iface.F", "len", c)
154180
}
155181

156182
func (ms *MySuite) TestStructStringValidation(c *C) {
@@ -173,6 +199,9 @@ func (ms *MySuite) TestStructStringValidation(c *C) {
173199
}{
174200
A: "1",
175201
},
202+
Iface: &Impl{
203+
F: "123",
204+
},
176205
}
177206

178207
err := validator.ValidateStruct(tSuccess)
@@ -193,6 +222,9 @@ func (ms *MySuite) TestStructStringValidation(c *C) {
193222
}{
194223
A: "",
195224
},
225+
Iface: &Impl{
226+
F: "12",
227+
},
196228
}
197229

198230
err = validator.ValidateStruct(tFail)
@@ -201,7 +233,7 @@ func (ms *MySuite) TestStructStringValidation(c *C) {
201233
c.Assert(err, NotNil)
202234
c.Assert(err.Struct, Equals, "TestString")
203235
c.Assert(len(err.Errors), Equals, 6)
204-
c.Assert(len(err.StructErrors), Equals, 2)
236+
c.Assert(len(err.StructErrors), Equals, 3)
205237

206238
// Assert Fields
207239
AssertFieldError(err, "Required", "required", c)

0 commit comments

Comments
 (0)