Required_If 封装中的组合问题 github.com/go-playground/validator/v10

Required_If Combination Issue in github.com/go-playground/validator/v10 package

提问人:Mohanraj M 提问时间:11/7/2023 最后编辑:Jonathan HallMohanraj M 更新时间:11/8/2023 访问量:39

问:

软件包版本,例如。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")
    }
}

无法找出代码或验证程序包中的问题所在。任何帮助将不胜感激......

struct slice go-gorm delve

评论


答:

1赞 Brits 11/7/2023 #1

添加 omitempty,例如:

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_ifPersonEntityPersonEntityomitemptystructrequired_ifstruct

另一种选择是使用指针 (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"`
}

这里的区别在于,当没有时,该值将是(与具有默认值的 a 相反),这意味着没有任何东西可以验证。EntitynilEntityvalidator

Nots:我建议使用(根据文档)。v := validator.New(validator.WithRequiredStructEnabled())

评论

0赞 Mohanraj M 11/7/2023
@Britis 您能解释一下我们为什么在这里使用指针以及它是如何工作的吗?omitempty 对我不起作用,它仍然抛出验证错误
0赞 Mohanraj M 11/7/2023
验证错误:密钥:“Application.Applicants[0]。Entity.Name 错误:在“必需”标记键上对“名称”的字段验证失败:“Application.Applicants[0]。Entity.TaxID“错误:在”必需“标记键上”TaxID“的字段验证失败:”Application.Applicants[1]。Person.Name 错误:在“必需”标记键上对“名称”的字段验证失败:“Application.Applicants[1]。Person.Age' 错误:'Age' 的字段验证在“必需”标记键上失败:“Application.Applicants[1]。Person.Email“错误:添加 omitempty 后,在”required“标记上对”电子邮件“的字段验证失败,此错误
0赞 Brits 11/8/2023
对不起 - 我不能复制这个。我添加了 playground 链接,以便您可以运行我的完整示例(您可能需要将代码复制到本地计算机才能运行它)。