Skip to content

Commit

Permalink
Call Validate on custom slice types. Fixes elastic#133.
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake Rouse authored and Blake Rouse committed Jan 14, 2020
1 parent c5859d3 commit 184566b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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 184566b

Please sign in to comment.