Skip to content

Commit b74dbde

Browse files
authored
Merge pull request #1 from ladydascalie/feature/fix-embedded-structs
Fix embedded structs
2 parents 00b4fc7 + 76b5e43 commit b74dbde

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

v.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ func Struct(structure interface{}) error {
5656
// recurse if this is an embedded struct
5757
if value.Kind() == reflect.Struct && field.PkgPath == "" {
5858
// only exported fields should do this
59-
return Struct(value.Interface())
59+
if err := Struct(value.Interface()); err != nil {
60+
return err
61+
}
6062
}
6163

6264
// get all the v tags

v_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,25 @@ func TestStruct(t *testing.T) {
130130
}
131131
}
132132

133+
func Test_Embedding(t *testing.T) {
134+
type A struct {
135+
StrField string `v:"between:0..10"`
136+
}
137+
type B struct {
138+
A
139+
IntField int `v:"between:0..10"`
140+
}
141+
err := Struct(B{
142+
A: A{
143+
StrField: "hello world!", // this is not ok
144+
},
145+
IntField: 10, // this is ok
146+
})
147+
if err == nil {
148+
t.Error("This should fail")
149+
}
150+
}
151+
133152
func Test_validationErorrs_Error(t *testing.T) {
134153
tests := []struct {
135154
name string

0 commit comments

Comments
 (0)