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

[BUG] Validation rules using enums are invalid #2890

Closed
shuqingzai opened this issue Aug 22, 2023 · 9 comments
Closed

[BUG] Validation rules using enums are invalid #2890

shuqingzai opened this issue Aug 22, 2023 · 9 comments
Assignees
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. done This issue is done, which may be release in next version.

Comments

@shuqingzai
Copy link

package main

type IEnum int

const (
	EnumUnknown IEnum = 0
	EnumEmail   IEnum = 1
	EnumName    IEnum = 2
)

type SEnums string

const (
	EnumUnknownStr SEnums = "unknown"
	EnumEmailStr   SEnums = "email"
	EnumNameStr    SEnums = "name"
)

type VEnumParam struct {
	IType IEnum  `json:"iType" v:"required|enums#类型不能为空|非允许的枚举数值Int"`
	SType SEnums `json:"sType" v:"required|enums#类型不能为空|非允许的枚举数值Str"`
}

func vEnums() {
	var (
		err error

		ctx = gctx.New()
		check = &VEnumParam{
			IType: EnumEmail,
			SType: EnumEmailStr,
		}
	)

	if err = g.Validator().Data(check).Run(ctx); err != nil {
		g.Dump(err.(gvalid.Error).Maps())
		g.Dump(gerror.Current(err))
		g.Log().Warning(ctx, "validator failed")

		return
	}

	g.Log().Info(ctx, "validator success")
}

func main() {
	vEnums()
}

然后使用 gf gen enums 生成 boot_enums 并导入进行验证

enums 验证报错

{
    "message": "\"enums\" validation rule can only be used in struct validation currently",
    "data": null
}

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

#-> % go version
go version go1.21.0 darwin/amd64

2. What version of GoFrame are you using?

#-> % gf version
GoFrame CLI Tool v2.5.1, https://goframe.org
GoFrame Version: v2.5.1 in current go.mod
CLI Installed At: /Code/go/bin/gf
CLI Built Detail:
  Go Version:  go1.20.4
  GF Version:  v2.5.1
  Git Commit:  2023-07-26 21:27:58 e0e00434cc87d6edf64fc3df40ce7d3f40758794
  Build Time:  2023-07-26 21:32:56

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

是的

4. What did you do?

5. What did you expect to see?

6. What did you see instead?

@Issues-translate-bot Issues-translate-bot changed the title [BUG] 验证规则使用 enums 无效 [BUG] Validation rules using enums are invalid Aug 22, 2023
@gqcn
Copy link
Member

gqcn commented Aug 23, 2023

@shuqingzai But I cannot reproduce this using latest v2.5.2, here're my codes:

package main

import (
	"fmt"
	"reflect"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/util/gtag"
	"github.com/gogf/gf/v2/util/gvalid"
)

type IEnum int

const (
	EnumUnknown IEnum = 0
	EnumEmail   IEnum = 1
	EnumName    IEnum = 2
)

type SEnums string

const (
	EnumUnknownStr SEnums = "unknown"
	EnumEmailStr   SEnums = "email"
	EnumNameStr    SEnums = "name"
)

type VEnumParam struct {
	IType IEnum  `json:"iType" v:"required|enums#类型不能为空|非允许的枚举数值Int"`
	SType SEnums `json:"sType" v:"required|enums#类型不能为空|非允许的枚举数值Str"`
}

func main() {
	var (
		err error

		ctx   = gctx.New()
		check = &VEnumParam{
			IType: EnumEmail,
			SType: EnumEmailStr,
		}
	)

	enumsContent := fmt.Sprintf(
		`{"%s":[0,1,2], "%s":["unknown","email","name"]}`,
		reflect.TypeOf(check.IType).String(), reflect.TypeOf(check.SType).String(),
	)

	err = gtag.SetGlobalEnums(enumsContent)
	if err != nil {
		panic(err)
	}

	if err = g.Validator().Data(check).Run(ctx); err != nil {
		g.Dump(err.(gvalid.Error).Maps())
		g.Dump(gerror.Current(err))
		g.Log().Warning(ctx, "validator failed")

		return
	}

	g.Log().Info(ctx, "validator success")
}

Here's my result:
image

@gqcn gqcn added the cannot reproduce We cannot reproduce it, it might resolved in new version or you should give more details. label Aug 23, 2023
@shuqingzai
Copy link
Author

@gqcn
我不确定是什么问题,验证时,这里是 map ,但是我的 enums 应该是没有问题的,API 文档也能显示出来

image

image

image

image

@greenhat616
Copy link

greenhat616 commented Aug 26, 2023

Same issue.
image

image

image

@Issues-translate-bot
Copy link

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


Same issue.
image

image

@shuqingzai
Copy link
Author

shuqingzai commented Aug 30, 2023

@gqcn 我不确定是什么问题,验证时,这里是 map ,但是我的 enums 应该是没有问题的,API 文档也能显示出来

image

image

image

image


@gqcn 这与

if err = gvalid.New().
赋值开始有关
data 是一个 map[string]any
data map[string]interface{}

且被赋值到 v.assoc

Assoc(data).

  1. v. assoc 被设置为 map[string]any ,且
    checkValueData = v.assoc // Ready to be validated data, which can be type of .
    赋值给 checkValueData
  2. 因为 checkValueData 被赋值导致
    if checkValueData == nil {
    判断不成立,无法把真正的参数类型字段 object 赋值到 checkValueData
  3. 后续在进行验证数据组合
    DataRaw: checkValueData,

    中把 checkValueData 传递给 doCheckValueInput.DataRaw ,而不是传递 object ,导致 DataRaw 是一个 map[string]any
  4. 真正执行验证时,无论是内置规则还是用户自定义规则都是把 DataRaw 转为 *gvar.Val ,实际是一个 错误的 map[string]any
    参考:

@gqcn
这应该只是 http 请求才能被测试出来,因为一般写测试不会设置 assoc 进行验证
现在 http 的验证无法进行 enums 规则验证或无法获取原始的参数类型

@Issues-translate-bot
Copy link

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


@gqcn I'm not sure what the problem is. When verifying, here is the map, but my enums should be fine, and the API documentation can also show it

image

image

image

image

@gqcn This is related to

if err = gvalid.New().
Assignment start
data is a map[string]any assigned to v.assoc
Assoc(data).

  1. v.assoc is set to map[string]any and
    checkValueData = v.assoc // Ready to be validated data, which can be type of .
    is assigned to checkValueData
  2. if checkValueData == nil {
    is invalid because checkValueData is assigned, and cannot assign the real parameter type field object to checkValueData in
  3. Subsequent verification data combination
    DataRaw: checkValueData,

    passing checkValueData to doCheckValueInput.DataRaw instead of passing object, resulting in DataRaw being a map[string]any
  4. When the verification is actually performed, whether it is a built-in rule or a user-defined rule, it converts DataRaw to *gvar.Val, which is actually a wrong map[string]any
    refer to:

@gqcn
This should only be tested for http requests, because generally writing tests will not set assoc for verification
Now the validation of http cannot perform enums rule validation or cannot get the original parameter type

@greenhat616
Copy link

please remove inactive label.

@gqcn
Copy link
Member

gqcn commented Oct 8, 2023

@shuqingzai 好的,我来构造一个http请求试试。

@gqcn gqcn added bug It is confirmed a bug, but don't worry, we'll handle it. wip and removed cannot reproduce We cannot reproduce it, it might resolved in new version or you should give more details. inactive labels Oct 8, 2023
@gqcn gqcn self-assigned this Oct 8, 2023
@Issues-translate-bot
Copy link

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


@shuqingzai Okay, let me try to construct an http request.

gqcn added a commit that referenced this issue Oct 8, 2023
@gqcn gqcn added done This issue is done, which may be release in next version. and removed wip labels Oct 8, 2023
@gqcn gqcn closed this as completed Oct 8, 2023
gqcn added a commit that referenced this issue Oct 9, 2023
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. done This issue is done, which may be release in next version.
Projects
None yet
Development

No branches or pull requests

4 participants