Validation 可选指针为uuid的Gin验证?

Validation 可选指针为uuid的Gin验证?,validation,go,go-gin,Validation,Go,Go Gin,我有这个结构 // CreateAccount Create Account Data Args type CreateAccount struct { .... RegistrationID string `example:"2c45e4ec-26e0-4043-86e4-c15b9cf985a2" json:"registration_id" binding:"max=63"` ParentID

我有这个结构

// CreateAccount Create Account Data Args
type CreateAccount struct {
    ....
    RegistrationID string  `example:"2c45e4ec-26e0-4043-86e4-c15b9cf985a2" json:"registration_id" binding:"max=63"`
    ParentID       *string `example:"7c45e4ec-26e0-4043-86e4-c15b9cf985a7" json:"parent_id" format:"uuid"`
}
ParentID是一个指针,它是可选的。但如果提供了,则应该是uuid

var params helper.CreateAccount
if err := ctx.ShouldBindJSON(&params); err != nil {
...
}

如果将此项添加到ParentID
绑定:“uuid”
,则ParentID将成为必填字段!但事实并非如此。如果且仅当给定了ParentID,则它应该是uuid。是否仍有这样的设置。

您必须先放置
忽略空的
标记

type CreateAccount struct {
    ....
    RegistrationID string  `json:"registration_id" binding:"max=63"`
    ParentID       *string `json:"parent_id" binding:"omitempty,uuid"`
}
从:

允许条件验证,例如,如果字段未设置值(由“必需”验证器确定)则其他验证(如最小值或最大值)将不会运行


我读这句话的方式是,
ommitempty
必须放在其他人之前,这样,如果值不等于零值,它就可以停止验证程序链。

您尝试过使用ommitempty吗?是的,如果我像这样使用它:“parent_id,ommitempty”
,则ParentID没有uuid验证。如果我像这样添加它:“parent\u id,ompitempty”绑定:“uuid”
,它将执行uuid验证。但是,它使ParentID成为必需的文件。