Skip to content

Commit 5250669

Browse files
committed
fix incorrect config key name
Fix bug with config validation incorrectly generating error messages omitting the field name segment resulting in the source of configuration problems not being obvious
1 parent 8bf0ec5 commit 5250669

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-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: 23 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,18 @@ func TestErrorFormatting(t *testing.T) {
121129
cfg := DefaultConfiguration()
122130
err := cfg.Validate()
123131
require.Error(t, err)
132+
124133
errortest.AssertError(t, err, commonerrors.ErrInvalid)
125-
assert.Contains(t, err.Error(), "invalid: structure failed validation: (TestConfig->db) [DUMMYCONFIG] cannot be blank")
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+
cfg := DefaultDeepConfiguration()
139+
err := cfg.Validate()
140+
require.Error(t, err)
141+
142+
errortest.AssertError(t, err, commonerrors.ErrInvalid)
143+
assert.Contains(t, err.Error(), "invalid: structure failed validation: (TestConfigDeep->TestConfig->db) [DEEP_CONFIG_DUMMYCONFIG_DB] cannot be blank")
126144
}
127145

128146
func TestServiceConfigurationLoad(t *testing.T) {
@@ -133,6 +151,9 @@ func TestServiceConfigurationLoad(t *testing.T) {
133151
err := Load("test", configTest, defaults)
134152
// Some required values are missing.
135153
require.Error(t, err)
154+
155+
assert.ErrorContains(t, err, "(TestConfig->db) [TEST_DUMMYCONFIG_DB] cannot be blank")
156+
136157
errortest.RequireError(t, err, commonerrors.ErrInvalid)
137158
errortest.RequireError(t, configTest.Validate(), commonerrors.ErrInvalid)
138159

0 commit comments

Comments
 (0)