必填_如果github.com/go-playground/validator/v10软件包中存在组合问题

qnakjoqk  于 2023-11-14  发布在  Go
关注(0)|答案(1)|浏览(235)

软件包版本如v9、v10:
软件包版本:v10

问题,问题或增强:当我试图运行下面的代码.我得到这个错误,这是有线

输出

  1. Validation error: Key: 'Application.Applicants[0].Entity.Name' Error:Field validation for 'Name' failed on the 'required' tag
  2. Key: 'Application.Applicants[0].Entity.TaxID' Error:Field validation for 'TaxID' failed on the 'required' tag
  3. Key: 'Application.Applicants[1].Person.Name' Error:Field validation for 'Name' failed on the 'required' tag
  4. Key: 'Application.Applicants[1].Person.Age' Error:Field validation for 'Age' failed on the 'required' tag
  5. Key: 'Application.Applicants[1].Person.Email' Error:Field validation for 'Email' failed on the 'required' tag

字符集

  • 代码示例,用于展示或复制:*
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/go-playground/validator/v10"
  5. )
  6. type Application struct {
  7. Applicants []Applicant `validate:"dive"`
  8. }
  9. type Applicant struct {
  10. ApplicantCategory string `validate:"required,oneof=PERSON ENTITY"`
  11. Person Person `validate:"required_if=ApplicantCategory PERSON"`
  12. Entity Entity `validate:"required_if=ApplicantCategory ENTITY"`
  13. }
  14. type Person struct {
  15. Name string `validate:"required"`
  16. Age int `validate:"required,gte=18"`
  17. Email string `validate:"required,email"`
  18. }
  19. type Entity struct {
  20. Name string `validate:"required"`
  21. TaxID string `validate:"required"`
  22. }
  23. func main() {
  24. // Create a new validator instance
  25. v := validator.New()
  26. // Create an instance of Application to validate
  27. data := Application{
  28. Applicants: []Applicant{
  29. {
  30. ApplicantCategory: "PERSON",
  31. Person: Person{
  32. Name: "John Doe",
  33. Age: 25,
  34. Email: "[email protected]",
  35. },
  36. },
  37. {
  38. ApplicantCategory: "ENTITY",
  39. Entity: Entity{
  40. Name: "Example Corp",
  41. TaxID: "123456789",
  42. },
  43. },
  44. },
  45. }
  46. // Use the validator to validate the Application struct and its Applicants
  47. if err := v.Struct(data); err != nil {
  48. fmt.Println("Validation error:", err)
  49. } else {
  50. fmt.Println("Validation passed")
  51. }
  52. }


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

zkure5ic

zkure5ic1#

添加omitempty,例如:

  1. type Applicant struct {
  2. ApplicantCategory string `validate:"required,oneof=PERSON ENTITY"`
  3. Person Person `validate:"required_if=ApplicantCategory PERSON,omitempty"`
  4. Entity Entity `validate:"required_if=ApplicantCategory ENTITY,omitempty"`
  5. }

字符集
playground中的完整示例(请注意,由于导入包的大小,此dos无法在playground中可靠运行)。
问题是required_if导致库检查Person/Entity是否存在,但库仍将验证空的Person/Entity添加omitempty意味着库将忽略空的struct;这提供了期望的结果,因为required_if将确保任何所需的struct不为空(意味着它将被验证)。
另一种选择是使用指针(playground):

  1. type Applicant struct {
  2. ApplicantCategory string `validate:"required,oneof=PERSON ENTITY"`
  3. Person *Person `validate:"required_if=ApplicantCategory PERSON"`
  4. Entity *Entity `validate:"required_if=ApplicantCategory ENTITY"`
  5. }


这里的区别是,当没有Entity时,值将是nil(与具有默认值的Entity相反),这意味着validator没有任何东西需要验证。
注:我建议使用v := validator.New(validator.WithRequiredStructEnabled())(根据文档)。

展开查看全部

相关问题