Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(util/gvalid): retrive empty slice parameter in custom validation rule function failed #3795

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions util/gvalid/gvalid_validator_check_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,14 @@ func (v *Validator) doCheckStruct(ctx context.Context, object interface{}) Error
if value != nil {
switch checkRuleItem.FieldKind {
case reflect.Struct, reflect.Map:
// empty struct or map.
if gconv.String(value) == emptyJsonObjectStr {
value = ""
value = nil
}
case reflect.Slice, reflect.Array:
// empty slice.
if gconv.String(value) == emptyJsonArrayStr {
value = ""
value = []any{}
}
default:
}
Expand Down
80 changes: 80 additions & 0 deletions util/gvalid/gvalid_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ package gvalid_test

import (
"context"
"fmt"
"testing"
"time"

"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
"github.com/gogf/gf/v2/util/gvalid"
)

Expand All @@ -34,3 +41,76 @@ func Test_Issue2503(t *testing.T) {
t.Fatal(err)
}
}

type Issue3636SliceV struct{}

func init() {
rule := Issue3636SliceV{}
gvalid.RegisterRule(rule.Name(), rule.Run)
}

func (r Issue3636SliceV) Name() string {
return "slice-v"
}

func (r Issue3636SliceV) Message() string {
return "not a slice"
}

func (r Issue3636SliceV) Run(_ context.Context, in gvalid.RuleFuncInput) error {
for _, v := range in.Value.Slice() {
if v == "" {
return gerror.New("empty value")
}
}
if !in.Value.IsSlice() {
return gerror.New("not a slice")
}
return nil
}

type Issue3636HelloReq struct {
g.Meta `path:"/hello" method:"POST"`

Name string `json:"name" v:"required" dc:"Your name"`
S []string `json:"s" v:"slice-v" dc:"S"`
}
type Issue3636HelloRes struct {
Name string `json:"name" v:"required" dc:"Your name"`
S []string `json:"s" v:"slice-v" dc:"S"`
}

type Issue3636Hello struct{}

func (Issue3636Hello) Say(ctx context.Context, req *Issue3636HelloReq) (res *Issue3636HelloRes, err error) {
res = &Issue3636HelloRes{
Name: req.Name,
S: req.S,
}
return
}

// https://github.com/gogf/gf/issues/3636
func Test_Issue3636(t *testing.T) {
s := g.Server(guid.S())
s.Use(ghttp.MiddlewareHandlerResponse)
s.Group("/", func(group *ghttp.RouterGroup) {
group.Bind(
new(Issue3636Hello),
)
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)

gtest.C(t, func(t *gtest.T) {
c := g.Client()
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
t.Assert(
c.PostContent(ctx, "/hello", `{"name": "t", "s" : []}`),
`{"code":0,"message":"","data":{"name":"t","s":[]}}`,
)
})
}
Loading