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

util/gvalid: When customizing validation rules, passing an empty array will become a string #3636

Open
shuqingzai opened this issue Jun 7, 2024 · 2 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@shuqingzai
Copy link

shuqingzai commented Jun 7, 2024

Go version

go version go1.21.10 darwin/amd64

GoFrame version

2.7.1

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

参考 自定义规则-规则注册 自定义规则并使用http请求访问,使用该规则

自行编写一个 http 服务接收参数

server:
  address:     ":8199"
  openapiPath: "/api.json"
  swaggerPath: "/swagger"
package main

import (
	"context"
	"errors"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/util/gvalid"
)


type sliceV struct{}

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

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

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

func (r sliceV) Run(_ context.Context, in gvalid.RuleFuncInput) error {
	g.Dump(in.Value)
	for _, v := range in.Value.Slice() {
		if v == "" {
			return errors.New("empty value")
		}
	}

	if !in.Value.IsSlice() {
		return errors.New("not a slice")
	}

	return nil
}

type HelloReq 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 HelloRes struct {
	Name string   `json:"name" v:"required" dc:"Your name"`
	S    []string `json:"s" v:"slice-v" dc:"S"`
}

type Hello struct {
}

func (Hello) Say(ctx context.Context, req *HelloReq) (res *HelloRes, err error) {
	g.Log().Debugf(ctx, `receive say: %+v`, req)
	res = &HelloRes{
		Name: req.Name,
		S:    req.S,
	}
	return
}

func main() {
	s := g.Server()
	s.Use(ghttp.MiddlewareHandlerResponse)
	s.Group("/", func(group *ghttp.RouterGroup) {
		group.Bind(
			new(Hello),
		)
	})
	s.Run()
}

使用如下参数访问

curl --location --request POST 'http://127.0.0.1:8199/hello' \
--header 'Content-Type: application/json' \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1:8199' \
--header 'Connection: keep-alive' \
--data-raw '{
    "name": "t",
    "s" : []
}'

What did you see happen?

错误的参数解析

image

What did you expect to see?

空 slice 的 value 不应该被视为空字符串

@shuqingzai shuqingzai added the bug It is confirmed a bug, but don't worry, we'll handle it. label Jun 7, 2024
@Issues-translate-bot Issues-translate-bot changed the title util/gvalid: 自定义验证规则时,传递空数组会变成字符串 util/gvalid: When customizing validation rules, passing an empty array will become a string Jun 7, 2024
@wlynxg
Copy link
Contributor

wlynxg commented Jun 26, 2024

The reason why Value is an empty string is to facilitate null value verification

if gconv.String(value) == emptyJsonArrayStr {

If you need the value of the original type, it is recommended to directly extract the corresponding value from in.Data for judgment.

@shuqingzai
Copy link
Author

The reason why Value is an empty string is to facilitate null value verification

if gconv.String(value) == emptyJsonArrayStr {

If you need the value of the original type, it is recommended to directly extract the corresponding value from in.Data for judgment.

in.Data 包含所有的请求数据,不能准确匹配对应的字段,你的解释表明这个 if gconv.String(value) == emptyJsonArrayStr { 处理逻辑有问题,改变了原始数据,引发BUG,应该被修复,而不是逃避

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

No branches or pull requests

2 participants