github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/inputvalidation/validators_map_test.go (about) 1 package inputvalidation_test 2 3 import ( 4 "testing" 5 6 validation "github.com/go-ozzo/ozzo-validation/v4" 7 "github.com/kyma-incubator/compass/components/director/pkg/inputvalidation" 8 "github.com/pkg/errors" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestEachKey(t *testing.T) { 13 structToValidate := struct { 14 String string 15 Map map[string]string 16 Nil map[string]string 17 Pointer *map[string]string 18 }{ 19 String: "test", 20 Map: map[string]string{ 21 "aaa": "bbbb", 22 "AAAA": "BBBB", 23 "Ę": "Ę", 24 }, 25 Nil: nil, 26 Pointer: &map[string]string{ 27 "aaa": "bbb", 28 }, 29 } 30 31 // GIVEN 32 testCases := []struct { 33 Name string 34 Rules []*validation.FieldRules 35 ExpectedError error 36 }{ 37 { 38 Name: "Success", 39 Rules: []*validation.FieldRules{ 40 validation.Field(&structToValidate.Map, inputvalidation.EachKey(validation.Required)), 41 }, 42 ExpectedError: nil, 43 }, 44 { 45 Name: "Success when map is nil", 46 Rules: []*validation.FieldRules{ 47 validation.Field(&structToValidate.Nil, inputvalidation.EachKey(validation.Required)), 48 }, 49 ExpectedError: nil, 50 }, 51 { 52 Name: "Success when pointer to map", 53 Rules: []*validation.FieldRules{ 54 validation.Field(&structToValidate.Map, inputvalidation.EachKey(validation.Required)), 55 }, 56 ExpectedError: nil, 57 }, 58 { 59 Name: "Works with custom validators", 60 Rules: []*validation.FieldRules{ 61 validation.Field(&structToValidate.Map, inputvalidation.EachKey(inputvalidation.DNSName)), 62 }, 63 ExpectedError: errors.New(dns1123Error), 64 }, 65 { 66 Name: "Returns error when field is not a map", 67 Rules: []*validation.FieldRules{ 68 validation.Field(&structToValidate.String, inputvalidation.EachKey(validation.Required)), 69 }, 70 ExpectedError: errors.New("String: the value must be a map."), 71 }, 72 { 73 Name: "Returns error when one map key is invalid", 74 Rules: []*validation.FieldRules{ 75 validation.Field(&structToValidate.Map, inputvalidation.EachKey(validation.Required, validation.Length(1, 3))), 76 }, 77 ExpectedError: errors.New("Map: (AAAA: the length must be between 1 and 3.)."), 78 }, 79 { 80 Name: "Returns error when multiple map keys are invalid", 81 Rules: []*validation.FieldRules{ 82 validation.Field(&structToValidate.Map, inputvalidation.EachKey(validation.Required, validation.Length(1, 2))), 83 }, 84 ExpectedError: errors.New("Map: (AAAA: the length must be between 1 and 2; aaa: the length must be between 1 and 2.)."), 85 }, 86 { 87 Name: "Returns error when pointer to invalid map", 88 Rules: []*validation.FieldRules{ 89 validation.Field(&structToValidate.Pointer, inputvalidation.EachKey(validation.Required, validation.Length(100, 200))), 90 }, 91 ExpectedError: errors.New("Pointer: (aaa: the length must be between 100 and 200.)."), 92 }, 93 } 94 95 for _, testCase := range testCases { 96 t.Run(testCase.Name, func(t *testing.T) { 97 // WHEN 98 err := validation.ValidateStruct(&structToValidate, testCase.Rules...) 99 // THEN 100 if testCase.ExpectedError == nil { 101 require.NoError(t, err) 102 } else { 103 require.Error(t, err) 104 require.Contains(t, err.Error(), testCase.ExpectedError.Error()) 105 } 106 }) 107 } 108 }