这是我的 struct :
type User struct {
Name `validate:"custom_validation"`
}
这是我的自定义验证:
func customFunc(fl validator.FieldLevel) bool {
// I want to get struct name inside here
// do some validations...
return true
}
validate.RegisterValidation("custom_validation", customFunc)
原因是我需要对数据库进行一些判断,我需要该数据库的表名,因此我需要 struct 名,因为表名类似于 struct 名.如果我硬编码了表名,这个customFunc
就不能用来在其他 struct 中进行验证.
我怎么能做到这一点?
参考文献:https://pkg.go.dev/github.com/go-playground/validator/v10#hdr-Custom_Validation_Functions