Skip to content

Commit 44bb790

Browse files
authored
🐛 DSGECO-1727 fix incorrect config key name (#738)
### Description Fix bug with config validation incorrectly generating error messages omitting the field name segment resulting in the source of configuration problems not being obvious ### Test Coverage - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent 8bf0ec5 commit 44bb790

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

changes/1727.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug with config validation incorrectly generating error messages omitting the field name segment

utils/config/error.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type validationError struct {
5656
mapStructureTree []string
5757
mapStructurePrefix *string
5858
reason string
59+
fieldName string
5960
}
6061

6162
func (v *validationError) GetTree() []string {
@@ -80,9 +81,11 @@ func (v *validationError) RecordField(fieldName string, mapStructureFieldName *s
8081
treeMap = append(treeMap, strings.ToUpper(strings.TrimSpace(*mapStructureFieldName)))
8182
treeMap = append(treeMap, v.mapStructureTree...)
8283
v.mapStructureTree = treeMap
84+
} else {
85+
v.fieldName = fieldName
8386
}
84-
v.mapStructurePrefix = mapStructurePrefix
8587

88+
v.mapStructurePrefix = mapStructurePrefix
8689
}
8790

8891
func (v *validationError) RecordPrefix(mapStructurePrefix string) {
@@ -114,6 +117,10 @@ func (v *validationError) GetMapStructurePath() string {
114117
if v.mapStructurePrefix != nil {
115118
mapstructureStr = fmt.Sprintf("%v_%v", strings.ToUpper(strings.TrimSpace(*v.mapStructurePrefix)), mapstructureStr)
116119
}
120+
121+
if v.fieldName != "" {
122+
mapstructureStr = fmt.Sprintf("%v_%v", mapstructureStr, strings.ToUpper(strings.TrimSpace(v.fieldName)))
123+
}
117124
}
118125
return mapstructureStr
119126
}

utils/config/service_configuration_test.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,15 @@ func DefaultDeepConfiguration() *DeepConfig {
8787
}
8888

8989
func (cfg *DeepConfig) Validate() error {
90-
return nil
90+
// Validate Embedded Structs
91+
err := ValidateEmbedded(cfg)
92+
if err != nil {
93+
return err
94+
}
95+
96+
return validation.ValidateStruct(cfg,
97+
validation.Field(&cfg.TestConfigDeep, validation.Required),
98+
)
9199
}
92100

93101
func (cfg *ConfigurationTest) Validate() error {
@@ -121,8 +129,37 @@ func TestErrorFormatting(t *testing.T) {
121129
cfg := DefaultConfiguration()
122130
err := cfg.Validate()
123131
require.Error(t, err)
132+
133+
errortest.AssertError(t, err, commonerrors.ErrInvalid)
134+
assert.Contains(t, err.Error(), "invalid: structure failed validation: (TestConfig->db) [DUMMYCONFIG_DB] cannot be blank")
135+
}
136+
137+
func TestDeepErrorFormatting(t *testing.T) {
138+
defaults := DefaultDeepConfiguration()
139+
err := defaults.Validate()
140+
require.Error(t, err)
141+
124142
errortest.AssertError(t, err, commonerrors.ErrInvalid)
125-
assert.Contains(t, err.Error(), "invalid: structure failed validation: (TestConfig->db) [DUMMYCONFIG] cannot be blank")
143+
assert.Contains(t, err.Error(), "invalid: structure failed validation: (TestConfigDeep->TestConfig->db) [DEEP_CONFIG_DUMMYCONFIG_DB] cannot be blank")
144+
145+
err = os.Setenv("TEST_DEEP_CONFIG_DUMMYCONFIG_DB", "a test db")
146+
require.NoError(t, err)
147+
err = os.Setenv("TEST_DEEP_CONFIG_DUMMYCONFIG_DUMMY_HOST", "a test host")
148+
require.NoError(t, err)
149+
err = os.Setenv("TEST_DEEP_CONFIG_DUMMYCONFIG_PASSWORD", "a test password")
150+
require.NoError(t, err)
151+
err = os.Setenv("TEST_DEEP_CONFIG_DUMMYCONFIG_USER", "a test user")
152+
require.NoError(t, err)
153+
err = os.Setenv("TEST_DEEP_CONFIG_DUMMY_CONFIG_DB", "a test user")
154+
require.NoError(t, err)
155+
156+
t.Run("defined mapstructure", func(t *testing.T) {
157+
configTest2 := &DeepConfig{}
158+
err = LoadFromSystem("test", configTest2, defaults)
159+
160+
errortest.AssertError(t, err, commonerrors.ErrInvalid)
161+
assert.Contains(t, err.Error(), "invalid: structure failed validation: (TestConfigDeep->TestConfig2->dummy_host) [TEST_DEEP_CONFIG_DUMMY_CONFIG_DUMMY_HOST] cannot be blank")
162+
})
126163
}
127164

128165
func TestServiceConfigurationLoad(t *testing.T) {
@@ -133,6 +170,9 @@ func TestServiceConfigurationLoad(t *testing.T) {
133170
err := Load("test", configTest, defaults)
134171
// Some required values are missing.
135172
require.Error(t, err)
173+
174+
assert.ErrorContains(t, err, "(TestConfig->db) [TEST_DUMMYCONFIG_DB] cannot be blank")
175+
136176
errortest.RequireError(t, err, commonerrors.ErrInvalid)
137177
errortest.RequireError(t, configTest.Validate(), commonerrors.ErrInvalid)
138178

0 commit comments

Comments
 (0)