简体   繁体   中英

Use Validator/Dive to enter an array and validate nested structs in go


type myType struct {
        value      int               `json:"value"`
        Name string            `json:"name" validate:"required"`
        URL     string            `json:"URL" validate:"required"`
        args     []otherType  `json:"args" validate:"dive", "required"`
} 

type otherType struct {
  name string `validate:"required"`
  origin string `validate:"required"`
}    


err := paramsValidator.Validate(someInstantiationOfThisStruct) 

Hello there. I'm a tad bit stumped on using validator's dive feature, This specific combination of validation scheme isn't present in the documentation for the validator. and I was unable to get it working with a little bit of tweaking.

I would like to simply enter the args array in the primary struct, and validate each of two sets of otherType. However I don't quite understand how this is supposed to transpire.

I understand dive incorrectly and it's not working of course, as the validator is unable to determine incorrect validations using Validate().

Is there any particular thing I'm doing wrong? In general how should I approach evaluating and validating args that are in an array?

I came searching for the answer here but the solution didn't work for me. In order to validate nested struct using go-playground/validator add dive.

So add below code to the nested struct at top level

`validate:"required,dive,required"`

Note: add without spaces, also make sure the fields are exposed (use PascalCase) to package incase u importing the struct

type myType struct {
        value      int               `json:"value"`
        Name string            `json:"name" validate:"required"`
        URL     string            `json:"URL" validate:"required"`
        Args     []OtherType  `json:"args" validate:"required,dive,required"`
} 

type OtherType struct {
  Name string `validate:"required"`
  Origin string `validate:"required"`
}  

Note: This validation is as per my use case where i want Args to be required and also want it to be exposed to other packages. Just trying to help other who come searching for the same issue as "Dive" is not documented properly in go/playground documentation

I was able to figure it out. I am so sorry for even posting, I was stumped for thirty minutes. but the solution was not that particularly bad.

type myType struct {
        value      int               `json:"value"`
        Name string            `json:"name" validate:"required"`
        URL     string            `json:"URL" validate:"required"`
        args     []otherType  `json:"args" validate:"dive", "required"`
} 

type otherType struct {
  name string `validate:"required"`
  origin string `validate:"required"`
}    



is the updated code. There was a missing, between "dive" and "required", and I had posted code that read

`validate: "dive, required"

dyslexia sorry: :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM