You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have the simple code below in which my purpose in to only have 1 field present:
package main
import (
"fmt"
"github.com/go-playground/validator/v10"
)
type Test struct {
Test1 Sub `json:"test_one" yaml:"test_one" validate:"required_without_all=Test2 Test3"`
Test2 Sub `json:"test_two" yaml:"test_two" validate:"required_without_all=Test1 Test3"`
Test3 Sub `json:"test_three" yaml:"test_three" validate:"required_without_all=Test1 Test2"`
}
type Sub struct {
Key string `json:"key" yaml:"key" validate:"required"`
Value string `json:"value" yaml:"value" validate:"required"`
}
var validate *validator.Validate
func main() {
validate = validator.New()
var z Test
z.Test1.Key = "test1"
z.Test1.Value = "test1"
// z.Test2 = "test2"
// z.Test3 = "test3"
err := validate.Struct(z)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("passing")
}
}
The problem is z.Test2.XXX and z.Test3.XXX are not present, so the validation is faling:
go run main.go
Key: 'Test.Test2.Key' Error:Field validation for 'Key' failed on the 'required' tag
Key: 'Test.Test2.Value' Error:Field validation for 'Value' failed on the 'required' tag
Key: 'Test.Test3.Key' Error:Field validation for 'Key' failed on the 'required' tag
Key: 'Test.Test3.Value' Error:Field validation for 'Value' failed on the 'required' tag
I don't want to add structonly,required_without_all because I want that the provided field get validated.
Any ideas how I can do that?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello guys,
I have the simple code below in which my purpose in to only have 1 field present:
The problem is
z.Test2.XXX
andz.Test3.XXX
are not present, so the validation is faling:I don't want to add
structonly,required_without_all
because I want that the provided field get validated.Any ideas how I can do that?
Beta Was this translation helpful? Give feedback.
All reactions