common/helpers: test validation of SubnetMap with a complex type

This commit is contained in:
Vincent Bernat
2023-09-05 23:05:42 +02:00
parent 2ba10b0195
commit e842202b82
2 changed files with 46 additions and 1 deletions

View File

@@ -79,6 +79,15 @@ func TestSubnetMapValidator(t *testing.T) {
type SomeStruct struct {
Blip *helpers.SubnetMap[string] `validate:"min=2,dive,min=3"`
}
type NestedStruct struct {
First string `validate:"required"`
Second string `validate:"oneof=one two"`
}
type OtherStruct struct {
Blip *helpers.SubnetMap[NestedStruct] `validate:"omitempty,dive"`
}
helpers.RegisterSubnetMapValidation[NestedStruct]()
cases := []struct {
Description string
@@ -110,6 +119,42 @@ func TestSubnetMapValidator(t *testing.T) {
}),
},
Error: true,
}, {
Description: "Valid OtherStruct",
Value: OtherStruct{
Blip: helpers.MustNewSubnetMap(map[string]NestedStruct{
"2001:db8::/64": {
First: "hello",
Second: "two",
},
}),
},
}, {
Description: "Empty OtherStruct",
Value: OtherStruct{
Blip: helpers.MustNewSubnetMap(map[string]NestedStruct{}),
},
}, {
Description: "OtherStruct missing required in NestedStruct",
Value: OtherStruct{
Blip: helpers.MustNewSubnetMap(map[string]NestedStruct{
"2001:db8::/64": {
Second: "two",
},
}),
},
Error: true,
}, {
Description: "OtherStruct incorrect NestedStruct",
Value: OtherStruct{
Blip: helpers.MustNewSubnetMap(map[string]NestedStruct{
"2001:db8::/64": {
First: "hello",
Second: "three",
},
}),
},
Error: true,
},
}
for _, tc := range cases {