首頁 > 後端開發 > Golang > 主體

github.com/go-playground/validator/v10 套件中的 required_if 組合問題

WBOY
發布: 2024-02-11 11:54:08
轉載
436 人瀏覽過

github.com/go-playground/validator/v10 包中的 required_if 组合问题

php小編草在這裡介紹一個關於github.com/go-playground/validator/v10套件中的required_if組合問題。在使用這個套件進行資料驗證時,有時我們需要根據某個欄位的值來判斷其他欄位是否必填。這時就可以使用required_if組合規則來實現這個需求。它可以根據指定的條件來決定欄位是否必填,非常靈活實用。在本文中,我們將詳細介紹如何使用required_if組合規則來解決這個問題。

問題內容

軟體包版本,例如。 v9、v10:

軟體套件版本:v10

問題、問題或改進: 當我嘗試運行下面的程式碼時。我收到此錯誤,這是有線的

輸出

Validation error: Key: 'Application.Applicants[0].Entity.Name' Error:Field validation for 'Name' failed on the 'required' tag
Key: 'Application.Applicants[0].Entity.TaxID' Error:Field validation for 'TaxID' failed on the 'required' tag
Key: 'Application.Applicants[1].Person.Name' Error:Field validation for 'Name' failed on the 'required' tag
Key: 'Application.Applicants[1].Person.Age' Error:Field validation for 'Age' failed on the 'required' tag
Key: 'Application.Applicants[1].Person.Email' Error:Field validation for 'Email' failed on the 'required' tag
登入後複製

用於展示或重現的程式碼範例:

package main

import (
    "fmt"
    "github.com/go-playground/validator/v10"
)

type Application struct {
    Applicants []Applicant `validate:"dive"`
}

type Applicant struct {
    ApplicantCategory string `validate:"required,oneof=PERSON ENTITY"`
    Person            Person `validate:"required_if=ApplicantCategory PERSON"`
    Entity            Entity `validate:"required_if=ApplicantCategory ENTITY"`
}

type Person struct {
    Name  string `validate:"required"`
    Age   int    `validate:"required,gte=18"`
    Email string `validate:"required,email"`
}

type Entity struct {
    Name  string `validate:"required"`
    TaxID string `validate:"required"`
}

func main() {
    // Create a new validator instance
    v := validator.New()

    // Create an instance of Application to validate
    data := Application{
        Applicants: []Applicant{
            {
                ApplicantCategory: "PERSON",
                Person: Person{
                    Name:  "John Doe",
                    Age:   25,
                    Email: "[email protected]",
                },
            },
            {
                ApplicantCategory: "ENTITY",
                Entity: Entity{
                    Name:  "Example Corp",
                    TaxID: "123456789",
                },
            },
        },
    }

    // Use the validator to validate the Application struct and its Applicants
    if err := v.Struct(data); err != nil {
        fmt.Println("Validation error:", err)
    } else {
        fmt.Println("Validation passed")
    }
}
登入後複製

無法找出程式碼或驗證程式包中的問題。任何幫助將不勝感激...

解決方法

添加 omitempty 例如:< /p>

type Applicant struct {
    ApplicantCategory string `validate:"required,oneof=PERSON ENTITY"`
    Person            Person `validate:"required_if=ApplicantCategory PERSON,omitempty"`
    Entity            Entity `validate:"required_if=ApplicantCategory ENTITY,omitempty"`
}
登入後複製

playground 中的完整範例(請注意,由於大小,這在 Playground 中無法可靠地執行導入包的數量)。

問題是required_if 導致函式庫檢查Person//Entity 是否存在,但函式庫仍會驗證空的Person/Entity (並且失敗!)。新增omitempty 意味著函式庫將忽略空的struct;這提供了所需的結果,因為required_if 將確保任何必要的struct 不為空(意味著它將被驗證)。

另一個選擇是使用指標(playground):

type Applicant struct {
    ApplicantCategory string `validate:"required,oneof=PERSON ENTITY"`
    Person            *Person `validate:"required_if=ApplicantCategory PERSON"`
    Entity            *Entity `validate:"required_if=ApplicantCategory ENTITY"`
}
登入後複製

這裡的差異在於,當沒有Entity 時,該值將為nil (與具有預設值的Entity 相反),這表示validator 無法進行驗證。

注意:我建議使用 v := validator.New(validator.WithRequiredStructEnabled()) (依照 文件)。

以上是github.com/go-playground/validator/v10 套件中的 required_if 組合問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!