Skip to content

Commit 7e7dbca

Browse files
authored
Call Validate on custom slice types. Fixes #133. (#134)
* Call Validate on custom slice types. Fixes #133. * Add changelog entry.
1 parent d8f3a42 commit 7e7dbca

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1313
### Removed
1414

1515
### Fixed
16+
- Call Validate on custom slice types. #133
1617

1718
## [0.7.0]
1819

reify.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,11 @@ func reifyDoArray(
577577
return reflect.Value{}, raiseValidation(ctx, val.meta(), "", err)
578578
}
579579

580+
if err := tryValidate(to); err != nil {
581+
ctx := val.Context()
582+
return reflect.Value{}, raiseValidation(ctx, val.meta(), "", err)
583+
}
584+
580585
return to, nil
581586
}
582587

validator_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
)
2828

2929
type myNonzeroInt int
30+
type myCustomList []int
3031

3132
type structValidator struct{ I int }
3233
type ptrStructValidator struct{ I int }
@@ -37,6 +38,13 @@ func (m myNonzeroInt) Validate() error {
3738
return testZeroErr(int(m))
3839
}
3940

41+
func (l myCustomList) Validate() error {
42+
if len(l) == 0 {
43+
return errZeroTest
44+
}
45+
return nil
46+
}
47+
4048
func (s structValidator) Validate() error {
4149
return testZeroErr(s.I)
4250
}
@@ -59,6 +67,7 @@ func TestValidationPass(t *testing.T) {
5967
"i": 5,
6068
"d": -10,
6169
"f": 3.14,
70+
"l": []int{0, 1},
6271
})
6372

6473
tests := []interface{}{
@@ -163,6 +172,11 @@ func TestValidationPass(t *testing.T) {
163172
X time.Duration `config:"f" validate:"min=3, max=20"`
164173
}{},
165174

175+
// validate field 'l'
176+
&struct {
177+
L myCustomList
178+
}{},
179+
166180
// other
167181
&struct {
168182
X int // field not present in config, but not required
@@ -184,6 +198,7 @@ func TestValidationFail(t *testing.T) {
184198
"i": 0,
185199
"d": -10,
186200
"f": 3.14,
201+
"l": []int{},
187202
})
188203

189204
tests := []interface{}{
@@ -261,6 +276,11 @@ func TestValidationFail(t *testing.T) {
261276
X time.Duration `config:"f" validate:"min=20s"`
262277
}{},
263278

279+
// test field 'l'
280+
&struct {
281+
X myCustomList `config:"l"`
282+
}{},
283+
264284
// other
265285
&struct {
266286
X int `validate:"required"`

0 commit comments

Comments
 (0)