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

required_if 中,当指定的 anotherField 为 uint8 类型时,校验失效 #245

Closed
henryjhenry opened this issue Nov 3, 2023 · 2 comments
Assignees
Labels
bug_fixed bug Something isn't working

Comments

@henryjhenry
Copy link

henryjhenry commented Nov 3, 2023

System :

  • OS: macOS
  • GO Version: 1.19
  • Pkg Version: 1.5.1

Describe the bug

required_if 中,当指定的 anotherField 为 uint8 类型时,校验失效.
我的待验证结构体为

type MyStruc struct {
	ID     int64    `json:"id" validate:"required"`
	Type   uint8    `json:"ip_type" validate:"in:0,1"`      
	IpList []string `json:"ip_list" validate:"required|validateIP"`
	Field  int      `json:"field" validate:"required_if:Type,1"`
}

我指定了,当 Type=1 时,Field 是必须的,但实际上此条 required_if 并为生效。

原因
阅读源码,发现在进行 required_if 校验时,调用 convTypeByBaseKind 方法将结构体 tag 中的值进行类型转换为目标类型并与目标值对比时,方法中并未做 uint8 的类型转换,截图如下:
image

我将短暂的将我的 Type 类型设置为 int 以解决我的问题。

@inhere inhere added the enhancement New feature or request label Jan 5, 2024
@inhere
Copy link
Member

inhere commented Jan 5, 2024

已修复,下个版本发布

@inhere inhere added bug Something isn't working and removed enhancement New feature or request labels Jan 5, 2024
inhere added a commit that referenced this issue Jan 5, 2024

Verified

This commit was signed with the committer’s verified signature.
hhatto Hideo Hattori
@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