Skip to content

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

Closed
@henryjhenry

Description

@henryjhenry

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 以解决我的问题。

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions