Skip to content

Commit 6268452

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #205 from joeybloggs/v8-development
Added Helper method to Report errors from struct level.
2 parents 885fd6c + 2940340 commit 6268452

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

validator.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ type StructLevel struct {
8787
v *Validate
8888
}
8989

90+
// ReportValidationErrors accepts the key relative to the top level struct and validatin errors.
91+
// Example: had a triple nested struct User, ContactInfo, Country and ran errs := validate.Struct(country)
92+
// from within a User struct level validation would call this method like so:
93+
// ReportValidationErrors("ContactInfo.", errs)
94+
func (sl *StructLevel) ReportValidationErrors(relativeKey string, errs ValidationErrors) {
95+
for _, e := range errs {
96+
sl.errs[sl.errPrefix+relativeKey+e.Field] = e
97+
}
98+
}
99+
90100
// ReportError reports an error just by passing the field and tag information
91101
// NOTE: tag can be an existing validation tag or just something you make up
92102
// and precess on the flip side it's up to you.

validator_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,60 @@ func StructValidationTestStructInvalid(v *Validate, structLevel *StructLevel) {
270270
}
271271
}
272272

273+
func StructValidationTestStructReturnValidationErrors(v *Validate, structLevel *StructLevel) {
274+
275+
s := structLevel.CurrentStruct.Interface().(TestStructReturnValidationErrors)
276+
277+
errs := v.Struct(s.Inner1.Inner2)
278+
if errs == nil {
279+
return
280+
}
281+
282+
structLevel.ReportValidationErrors("Inner1.", errs.(ValidationErrors))
283+
}
284+
285+
type TestStructReturnValidationErrorsInner2 struct {
286+
String string `validate:"required"`
287+
}
288+
289+
type TestStructReturnValidationErrorsInner1 struct {
290+
Inner2 *TestStructReturnValidationErrorsInner2
291+
}
292+
293+
type TestStructReturnValidationErrors struct {
294+
Inner1 *TestStructReturnValidationErrorsInner1
295+
}
296+
297+
func TestStructLevelReturnValidationErrors(t *testing.T) {
298+
config := &Config{
299+
TagName: "validate",
300+
}
301+
302+
v1 := New(config)
303+
v1.RegisterStructValidation(StructValidationTestStructReturnValidationErrors, TestStructReturnValidationErrors{})
304+
305+
inner2 := &TestStructReturnValidationErrorsInner2{
306+
String: "I'm HERE",
307+
}
308+
309+
inner1 := &TestStructReturnValidationErrorsInner1{
310+
Inner2: inner2,
311+
}
312+
313+
val := &TestStructReturnValidationErrors{
314+
Inner1: inner1,
315+
}
316+
317+
errs := v1.Struct(val)
318+
Equal(t, errs, nil)
319+
320+
inner2.String = ""
321+
322+
errs = v1.Struct(val)
323+
NotEqual(t, errs, nil)
324+
AssertError(t, errs, "TestStructReturnValidationErrors.Inner1.Inner2.String", "String", "required")
325+
}
326+
273327
func TestStructLevelValidations(t *testing.T) {
274328

275329
config := &Config{

0 commit comments

Comments
 (0)