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
。
問題中的原始程式碼與ozzo 驗證 v4 傳回在結構中找不到欄位 #0。除此之外,您應該將以下程式碼新增至 (myotherstruct).validate
:
validation.field(&m.mystruct),
嘗試這個示範(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()) }
請注意,如果您呼叫(mystruct).validate
,則(myotherstruct).validate
不會被調用,因為前者對myotherstruct
一無所有所知。
以上是使用 ozzo 驗證不會呼叫嵌入結構的驗證方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!