php小编鱼仔发现,使用ozzo验证库可以轻松实现数据验证,但有时候可能会遇到需要调用嵌入结构的验证方法的情况。然而,ozzo验证库的设计原则是避免调用嵌入结构的验证方法,以确保代码的简洁性和可读性。这是因为嵌入结构的验证方法可能导致验证逻辑的复杂化,并且会增加代码的维护难度。因此,在使用ozzo验证库时,我们应该遵循这一原则,选择适合的验证方式,以保持代码的清晰和易于维护。
我正在使用 "github.com/go-ozzo/ozzo-validation/v4"
。
这些是我的结构:
type mystruct struct { uuid string `json:"uuid"` firstuuid string `json:"first_uuid"` seconduuid string `json:"second_uuid"` thirduuid string `json:"third_uuid"` phonenumber string `json:"phone_number"` email string `json:"email"` skypeid string `json:"skype_id"` } type myotherstruct struct { mystruct city string `json:"city"` comment string `json:"comment"` personnelid string `json:"personnel_id"` firstdate string `json:"first_date"` seconddate string `json:"second_date"` firstboolean bool `json:"first_boolean"` secondboolean bool `json:"second_boolean"` }
这些是验证方法:
func (m mystruct) validate() error { fmt.println("calling mystruct validator") err := validation.validatestruct( validation.field(&uui.uuid, is.uuid), validation.field(&uui.firstuuid, validation.required, is.uuid), validation.field(&uui.seconduuid, validation.required, is.uuid), validation.field(&uui.thirduuid, validation.required, is.uuid), validation.field(&uui.email, validation.required, is.email), validation.field(&uui.phonenumber, validation.required, validation.match(mobileregexp)), validation.field(&uui.skypeid, validation.required), ) return err } func (m myotherstruct) validate() error { fmt.println("calling myotherstruct validator") err := validation.validatestruct( validation.field(&uui.personnelid, validation.match(personnelidregexp)), validation.field(&uui.city, validation.required), validation.field(&uui.comment), validation.field(&uui.firstdate, validation.date(time.dateonly)), validation.field(&uui.seconddate, validation.date(time.dateonly)), ) return err }
这是我发送的请求:
{ "uuid": "1e57ef49-352f-4545-a43a-b51cad6c5a0a", "phone_number": "09124567891", "email": "[email protected]", "skype_id": "some_skype_id", "city": "a city", "personnel_id": "", "comment": "no comment for now!", "first_date": "", "second_date": "", "first_uuid": "94038913-2bdb-4dde-99fb-640a24e1c003", "second_uuid": "7fa0e242-841b-4de0-a3ce-e2b54ecd1bca", "third_uuid": "35ab6711-852e-42c8-aab3-dfb901a845f5", "first_boolean": true, "second_boolean": false }
我希望针对此问题调用 func (m mystruct) validate()
,但我没有收到日志 calling myotherstruct validator
。func (m mystruct) validate()
,但我没有收到日志 calling myotherstruct validator
。
问题中的源代码与ozzo 验证 v4 返回在结构中找不到字段 #0。除此之外,您应该将以下代码添加到 (myotherstruct).validate
问题中的源代码与ozzo 验证 v4 返回在结构中找不到字段 #0。除此之外,您应该将以下代码添加到 (myotherstruct).validate
:
validation.field(&m.mystruct),
尝试这个演示((mystruct).validate
,则 (myotherstruct).validate
不会被调用,因为前者对 myotherstruct
https://www.php.cn/link/9f29450d2eb58feb555078bdefe28aa5
package main import ( "fmt" validation "github.com/go-ozzo/ozzo-validation/v4" "github.com/go-ozzo/ozzo-validation/v4/is" ) type MyStruct struct { UUID string `json:"uuid"` Email string `json:"email"` } func (m MyStruct) Validate() error { fmt.Println("calling MyStruct validator") err := validation.ValidateStruct( &m, validation.Field(&m.UUID, is.UUID), validation.Field(&m.Email, validation.Required, is.Email), ) return err } type MyOtherStruct struct { MyStruct City string `json:"city"` } func (m MyOtherStruct) Validate() error { fmt.Println("calling MyOtherStruct validator") err := validation.ValidateStruct( &m, validation.Field(&m.City, validation.Required), // The following line is necessary to make (MyStruct).Validate being called. validation.Field(&m.MyStruct), ) return err } func main() { data := MyOtherStruct{} fmt.Printf("validation error: %v\n", data.Validate()) }🎜请注意,如果您调用 🎜 一无所知。🎜
以上是使用 ozzo 验证不会调用嵌入结构的验证方法的详细内容。更多信息请关注PHP中文网其他相关文章!