validator icon indicating copy to clipboard operation
validator copied to clipboard

required_with with custom struct

Open MangataL opened this issue 3 years ago • 1 comments

  • [x] I have looked at the documentation here first?
  • [x] I have looked at the examples provided that may showcase my question here?

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

How can I use required_with tag on my custom struct. For example, like the structure shown below, when UseA is true, the validator validate the required_with tag on filed TestA, and when UseA is false, skip the validating on filed TestA.

Code sample, to showcase or reproduce:


type A struct {
	Size int `validate:"gt=1"`
	Num int `validate:"gt=1"`
}

type Test struct {
	TestA A `validate:"required_with=ByPage"`
	UseA bool
}

MangataL avatar Dec 24 '21 08:12 MangataL

I don't think this is supported right now with struct tags. You can get this however by creating your own custom validator function. See https://pkg.go.dev/github.com/go-playground/validator/v10#hdr-Custom_Validation_Functions

Probably can be marked as a duplicate of #491

zemzale avatar Jan 15 '22 15:01 zemzale