Skip to content

Commit 8040a52

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #219 from joeybloggs/v8-development
Add new Anonymous test to hit new code
2 parents 589c2ad + 934bef0 commit 8040a52

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

validator_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,60 @@ type TestStructReturnValidationErrors struct {
296296
Inner1 *TestStructReturnValidationErrorsInner1
297297
}
298298

299+
func TestAnonymous(t *testing.T) {
300+
301+
v2 := New(&Config{TagName: "validate", FieldNameTag: "json"})
302+
303+
type Test struct {
304+
Anonymous struct {
305+
A string `validate:"required" json:"EH"`
306+
}
307+
AnonymousB struct {
308+
B string `validate:"required" json:"BEE"`
309+
}
310+
anonymousC struct {
311+
c string `validate:"required" json:"SEE"`
312+
}
313+
}
314+
315+
tst := &Test{
316+
Anonymous: struct {
317+
A string `validate:"required" json:"EH"`
318+
}{
319+
A: "1",
320+
},
321+
AnonymousB: struct {
322+
B string `validate:"required" json:"BEE"`
323+
}{
324+
B: "",
325+
},
326+
anonymousC: struct {
327+
c string `validate:"required" json:"SEE"`
328+
}{
329+
c: "",
330+
},
331+
}
332+
333+
err := v2.Struct(tst)
334+
NotEqual(t, err, nil)
335+
336+
errs := err.(ValidationErrors)
337+
338+
Equal(t, len(errs), 1)
339+
AssertError(t, errs, "Test.AnonymousB.B", "B", "required")
340+
Equal(t, errs["Test.AnonymousB.B"].Field, "B")
341+
Equal(t, errs["Test.AnonymousB.B"].Name, "BEE")
342+
343+
s := struct {
344+
c string `validate:"required" json:"SEE"`
345+
}{
346+
c: "",
347+
}
348+
349+
err = v2.Struct(s)
350+
Equal(t, err, nil)
351+
}
352+
299353
func TestStructLevelReturnValidationErrors(t *testing.T) {
300354
config := &Config{
301355
TagName: "validate",

0 commit comments

Comments
 (0)