Skip to content

Commit

Permalink
util/gvalid: fix slice enums verification failed (#3566)
Browse files Browse the repository at this point in the history
  • Loading branch information
xyqweb authored May 7, 2024
1 parent 8e075fb commit 94c7185
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions util/gvalid/gvalid_z_unit_feature_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,10 @@ func Test_Enums(t *testing.T) {
Id int
Enums EnumsTest `v:"enums"`
}
type SliceParams struct {
Id int
Enums []EnumsTest `v:"foreach|enums"`
}

oldEnumsJson, err := gtag.GetGlobalEnums()
t.AssertNil(err)
Expand All @@ -1596,5 +1600,11 @@ func Test_Enums(t *testing.T) {
Enums: "c",
}).Run(ctx)
t.Assert(err, "The Enums value `c` should be in enums of: [\"a\",\"b\"]")

err = g.Validator().Data(&SliceParams{
Id: 1,
Enums: []EnumsTest{EnumsTestA, EnumsTestB},
}).Run(ctx)
t.AssertNil(err)
})
}
5 changes: 5 additions & 0 deletions util/gvalid/internal/builtin/builtin_enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package builtin
import (
"errors"
"fmt"
"reflect"

"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
Expand Down Expand Up @@ -47,6 +48,10 @@ func (r RuleEnums) Run(in RunInput) error {
pkgPath = in.ValueType.PkgPath()
typeName = in.ValueType.Name()
)
if in.ValueType.Kind() == reflect.Slice {
pkgPath = in.ValueType.Elem().PkgPath()
typeName = in.ValueType.Elem().Name()
}
if pkgPath == "" {
return gerror.NewCodef(
gcode.CodeInvalidOperation,
Expand Down

0 comments on commit 94c7185

Please # to comment.