From 184566b3bcb30ac52e17d4390f730065b1628fc2 Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Tue, 14 Jan 2020 09:08:10 -0500 Subject: [PATCH] Call Validate on custom slice types. Fixes #133. --- reify.go | 5 +++++ validator_test.go | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/reify.go b/reify.go index c209964..13a9c9c 100644 --- a/reify.go +++ b/reify.go @@ -577,6 +577,11 @@ func reifyDoArray( return reflect.Value{}, raiseValidation(ctx, val.meta(), "", err) } + if err := tryValidate(to); err != nil { + ctx := val.Context() + return reflect.Value{}, raiseValidation(ctx, val.meta(), "", err) + } + return to, nil } diff --git a/validator_test.go b/validator_test.go index a068076..8298ca0 100644 --- a/validator_test.go +++ b/validator_test.go @@ -27,6 +27,7 @@ import ( ) type myNonzeroInt int +type myCustomList []int type structValidator struct{ I int } type ptrStructValidator struct{ I int } @@ -37,6 +38,13 @@ func (m myNonzeroInt) Validate() error { return testZeroErr(int(m)) } +func (l myCustomList) Validate() error { + if len(l) == 0 { + return errZeroTest + } + return nil +} + func (s structValidator) Validate() error { return testZeroErr(s.I) } @@ -59,6 +67,7 @@ func TestValidationPass(t *testing.T) { "i": 5, "d": -10, "f": 3.14, + "l": []int{0, 1}, }) tests := []interface{}{ @@ -163,6 +172,11 @@ func TestValidationPass(t *testing.T) { X time.Duration `config:"f" validate:"min=3, max=20"` }{}, + // validate field 'l' + &struct { + L myCustomList + }{}, + // other &struct { X int // field not present in config, but not required @@ -184,6 +198,7 @@ func TestValidationFail(t *testing.T) { "i": 0, "d": -10, "f": 3.14, + "l": []int{}, }) tests := []interface{}{ @@ -261,6 +276,11 @@ func TestValidationFail(t *testing.T) { X time.Duration `config:"f" validate:"min=20s"` }{}, + // test field 'l' + &struct { + X myCustomList `config:"l"` + }{}, + // other &struct { X int `validate:"required"`