php Xiaobian Yuzai found that data verification can be easily achieved using the ozzo verification library, but sometimes you may encounter situations where you need to call the verification method of the embedded structure. However, the design principle of the ozzo validation library is to avoid calling validation methods embedded in structures to ensure code simplicity and readability. This is because the verification method embedded in the structure may complicate the verification logic and increase the difficulty of code maintenance. Therefore, when using the ozzo verification library, we should follow this principle and choose a suitable verification method to keep the code clear and easy to maintain.
I am using "github.com/go-ozzo/ozzo-validation/v4"
.
These are my structures:
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"` }
These are the verification methods:
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 }
This is the request I sent:
{ "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 }
I want to call func (m mystruct) validate()
for this problem, but I don't get the log calling myotherstruct validator
.
The source code in the question with ozzo validation v4 returns field #0 not found in structure. In addition to this, you should add the following code to (myotherstruct).validate
:
validation.field(&m.mystruct),
Try this demo (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()) }
Please note that if you call (mystruct).validate
, (myotherstruct).validate
will not be called because the former has nothing to do with myotherstruct
known.
The above is the detailed content of Validating with ozzo does not call the validation method of the embedded structure. For more information, please follow other related articles on the PHP Chinese website!