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

gvalid not working when use with standard router and int property be pass in an empty string #2786

Closed
WankkoRee opened this issue Jul 20, 2023 · 3 comments

Comments

@WankkoRee
Copy link
Contributor

WankkoRee commented Jul 20, 2023

1. What version of Go and system type/arch are you using?

go version go1.20.5 windows/amd64

2. What version of GoFrame are you using?

v2.5.0

3. Can this issue be re-produced with the latest release?

yes

4. What did you do?

package main

import (
	"context"
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
)

type TestReq struct {
	g.Meta `path:"/test" method:"get"`
	Test   *int `v:"integer|min:1" dc:"测试"`
}
type TestRes struct {
	RawQuery string
	GotQuery string
}

type Test struct{}

func (Test) Test(ctx context.Context, req *TestReq) (res *TestRes, err error) {
	res = &TestRes{
		RawQuery: g.RequestFromCtx(ctx).URL.RawQuery,
	}
	if req.Test == nil {
		res.GotQuery = fmt.Sprintf("%+v", req.Test)
	} else {
		res.GotQuery = fmt.Sprintf("%d", *req.Test)
	}
	return
}

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

5. What did you expect to see?

  1. GET /test

    {
        "code": 0,
        "message": "",
        "data": {
            "RawQuery": "",
            "GotQuery": "<nil>"
        }
    }
  2. GET /test?test=

    {
        "code": 51,
        "message": "The Test value `0` must be equal or greater than 1",
        "data": null
    }
  3. GET /test?test=0

    {
        "code": 51,
        "message": "The Test value `0` must be equal or greater than 1",
        "data": null
    }

6. What did you see instead?

  1. GET /test

    {
        "code": 0,
        "message": "",
        "data": {
            "RawQuery": "",
            "GotQuery": "<nil>"
        }
    }
  2. GET /test?test=

    {
        "code": 0,
        "message": "",
        "data": {
            "RawQuery": "test=",
            "GotQuery": "0"
        }
    }
  3. GET /test?test=0

    {
        "code": 51,
        "message": "The Test value `0` must be equal or greater than 1",
        "data": null
    }
@gqcn
Copy link
Member

gqcn commented Sep 25, 2023

@WankkoRee Hi, that is expected result. If a parameter is not required, it ignores the validation if such parameter is not committed. Try using this validation rule for attribute Test: Test *int `v:"required|integer|min:1" dc:"测试"`, and see the difference.

@gqcn gqcn added the question label Sep 25, 2023
@gqcn gqcn closed this as completed Sep 25, 2023
@WankkoRee
Copy link
Contributor Author

@gqcn 我认为这个结果是不符合通常认知的。当一个传参的定义是指针且非required时,如果这个参数被提交,那么获取到的值就应当有符合校验;如果一个传参没被提交,则应当是个nil

在我给出的demo中,GET /test代表明确不提交test参数,可以看到获取到的值是nil,符合认知;GET /test?test=0代表明确提交test参数,可以看到会命中校验逻辑,也符合认知;但是GET /test?test=的含义则不太明确,如果理解成提交了test参数,因为实际上确实获取到了值,但是值为0,说明并没有触发预期的校验,如果理解成没提交test参数,但是实际上却拿到了非nil的值。

@WankkoRee WankkoRee reopened this Oct 1, 2023
@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@gqcn I think this result is inconsistent with common understanding. When the definition of a passed parameter is a pointer and is not required, if the parameter is submitted, the value obtained should be verified; if a passed parameter is not submitted, it should be nil.

In the demo I gave, GET /test means explicitly not submitting the test parameter. You can see that the obtained value is nil, which is consistent with the understanding; GET /test?test=0 means explicitly submitting it. The test parameter can be seen to hit the verification logic, which is also consistent with the understanding; however, the meaning of GET /test?test= is not clear. If it is understood that the test parameter is submitted, it is actually obtained. The value is reached, but the value is 0, which means that the expected verification was not triggered. If it is understood that the test parameter was not submitted, but a non-nil value was actually obtained.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants