Skip to content

Commit

Permalink
Call Validate on custom slice types. Fixes #133. (#134)
Browse files Browse the repository at this point in the history
* Call Validate on custom slice types. Fixes #133.

* Add changelog entry.
  • Loading branch information
blakerouse authored Jan 14, 2020
1 parent d8f3a42 commit 7e7dbca
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Removed

### Fixed
- Call Validate on custom slice types. #133

## [0.7.0]

Expand Down
5 changes: 5 additions & 0 deletions reify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
20 changes: 20 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
)

type myNonzeroInt int
type myCustomList []int

type structValidator struct{ I int }
type ptrStructValidator struct{ I int }
Expand All @@ -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)
}
Expand All @@ -59,6 +67,7 @@ func TestValidationPass(t *testing.T) {
"i": 5,
"d": -10,
"f": 3.14,
"l": []int{0, 1},
})

tests := []interface{}{
Expand Down Expand Up @@ -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
Expand All @@ -184,6 +198,7 @@ func TestValidationFail(t *testing.T) {
"i": 0,
"d": -10,
"f": 3.14,
"l": []int{},
})

tests := []interface{}{
Expand Down Expand Up @@ -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"`
Expand Down

0 comments on commit 7e7dbca

Please sign in to comment.