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

使用 uint filter时,传 null 值会报错 convert value type error #246

Closed
devhaozi opened this issue Nov 8, 2023 · 4 comments
Closed
Assignees
Labels
bug_fixed bug Something isn't working

Comments

@devhaozi
Copy link

devhaozi commented Nov 8, 2023

System (please complete the following information):

  • OS: linux
  • GO Version: 1.21
  • Pkg Version: 1.5.1

Describe the bug

使用 uint filter时,传 null 值会报错 convert value type error。

规则没写 required,前端用的 select 选择,没选的时候会传 null 过来。

To Reproduce

// 类似这样的结构体和规则
type CertStore struct {
	Type      string   `form:"type" json:"type"`
	Domains   []string `form:"domains" json:"domains"`
	AutoRenew bool     `form:"auto_renew" json:"auto_renew"`
	UserID    uint     `form:"user_id" json:"user_id" filter:"uint"`
	DNSID     uint     `form:"dns_id" json:"dns_id" filter:"uint"`
	WebsiteID uint     `form:"website_id" json:"website_id" filter:"uint"`
}

func (r *CertStore) Rules(ctx http.Context) map[string]string {
	return map[string]string{
		"type":       "required|in:P256,P384,2048,4096",
		"domains":    "required|array",
		"auto_renew": "required|bool",
		"user_id":    "required|uint|exists:cert_users,id",
		"dns_id":     "uint",
		"website_id": "uint",
	}
}

Expected behavior

传 null 时能正常通过验证。

Screenshots

Additional context

如果不使用 filter,则没有这个报错,但是由于 json 自动把数字转成 float64 会导致结构体绑定失败。

@inhere inhere added enhancement New feature or request bug Something isn't working labels Dec 6, 2023
@inhere
Copy link
Member

inhere commented Dec 8, 2023

下个版本修复

@inhere
Copy link
Member

inhere commented Dec 13, 2023

类型不是限制了 uint 吗?怎么传 null 呢?

  • 能给下输入json数据示例吗?
  • 还有怎么绑定数据到结构体的

@inhere inhere removed the enhancement New feature or request label Dec 13, 2023
@devhaozi
Copy link
Author

devhaozi commented Dec 13, 2023

类型不是限制了 uint 吗?怎么传 null 呢?

  • 能给下输入json数据示例吗?
  • 还有怎么绑定数据到结构体的

比如前端做了个多选,不选的时候就是null,希望这时候能直接转成uint0

{
    "domains": [],
    "dns_id": null,
    "type": "P256",
    "user_id": null,
    "website_id": null,
    "auto_renew": true
}

website_id这项,没写required

绑定的报错就是数字被json.Unmarshal解析成float64,然后直接通过github.com/mitchellh/mapstructure无法转换到uint去,这个我已经自行解决。

@inhere
Copy link
Member

inhere commented Jan 24, 2024

@inhere inhere closed this as completed Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug_fixed bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants