Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/1727.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug with config validation incorrectly generating error messages omitting the field name segment
9 changes: 8 additions & 1 deletion utils/config/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type validationError struct {
mapStructureTree []string
mapStructurePrefix *string
reason string
fieldName string
}

func (v *validationError) GetTree() []string {
Expand All @@ -80,9 +81,11 @@ func (v *validationError) RecordField(fieldName string, mapStructureFieldName *s
treeMap = append(treeMap, strings.ToUpper(strings.TrimSpace(*mapStructureFieldName)))
treeMap = append(treeMap, v.mapStructureTree...)
v.mapStructureTree = treeMap
} else {
v.fieldName = fieldName
}
v.mapStructurePrefix = mapStructurePrefix

v.mapStructurePrefix = mapStructurePrefix
}

func (v *validationError) RecordPrefix(mapStructurePrefix string) {
Expand Down Expand Up @@ -114,6 +117,10 @@ func (v *validationError) GetMapStructurePath() string {
if v.mapStructurePrefix != nil {
mapstructureStr = fmt.Sprintf("%v_%v", strings.ToUpper(strings.TrimSpace(*v.mapStructurePrefix)), mapstructureStr)
}

if v.fieldName != "" {
mapstructureStr = fmt.Sprintf("%v_%v", mapstructureStr, strings.ToUpper(strings.TrimSpace(v.fieldName)))
}
}
return mapstructureStr
}
Expand Down
44 changes: 42 additions & 2 deletions utils/config/service_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ func DefaultDeepConfiguration() *DeepConfig {
}

func (cfg *DeepConfig) Validate() error {
return nil
// Validate Embedded Structs
err := ValidateEmbedded(cfg)
if err != nil {
return err
}

return validation.ValidateStruct(cfg,
validation.Field(&cfg.TestConfigDeep, validation.Required),
)
}

func (cfg *ConfigurationTest) Validate() error {
Expand Down Expand Up @@ -121,8 +129,37 @@ func TestErrorFormatting(t *testing.T) {
cfg := DefaultConfiguration()
err := cfg.Validate()
require.Error(t, err)

errortest.AssertError(t, err, commonerrors.ErrInvalid)
assert.Contains(t, err.Error(), "invalid: structure failed validation: (TestConfig->db) [DUMMYCONFIG_DB] cannot be blank")
}

func TestDeepErrorFormatting(t *testing.T) {
defaults := DefaultDeepConfiguration()
err := defaults.Validate()
require.Error(t, err)

errortest.AssertError(t, err, commonerrors.ErrInvalid)
assert.Contains(t, err.Error(), "invalid: structure failed validation: (TestConfig->db) [DUMMYCONFIG] cannot be blank")
assert.Contains(t, err.Error(), "invalid: structure failed validation: (TestConfigDeep->TestConfig->db) [DEEP_CONFIG_DUMMYCONFIG_DB] cannot be blank")

err = os.Setenv("TEST_DEEP_CONFIG_DUMMYCONFIG_DB", "a test db")
require.NoError(t, err)
err = os.Setenv("TEST_DEEP_CONFIG_DUMMYCONFIG_DUMMY_HOST", "a test host")
require.NoError(t, err)
err = os.Setenv("TEST_DEEP_CONFIG_DUMMYCONFIG_PASSWORD", "a test password")
require.NoError(t, err)
err = os.Setenv("TEST_DEEP_CONFIG_DUMMYCONFIG_USER", "a test user")
require.NoError(t, err)
err = os.Setenv("TEST_DEEP_CONFIG_DUMMY_CONFIG_DB", "a test user")
require.NoError(t, err)

t.Run("defined mapstructure", func(t *testing.T) {
configTest2 := &DeepConfig{}
err = LoadFromSystem("test", configTest2, defaults)

errortest.AssertError(t, err, commonerrors.ErrInvalid)
assert.Contains(t, err.Error(), "invalid: structure failed validation: (TestConfigDeep->TestConfig2->dummy_host) [TEST_DEEP_CONFIG_DUMMY_CONFIG_DUMMY_HOST] cannot be blank")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for this

})
}

func TestServiceConfigurationLoad(t *testing.T) {
Expand All @@ -133,6 +170,9 @@ func TestServiceConfigurationLoad(t *testing.T) {
err := Load("test", configTest, defaults)
// Some required values are missing.
require.Error(t, err)

assert.ErrorContains(t, err, "(TestConfig->db) [TEST_DUMMYCONFIG_DB] cannot be blank")

errortest.RequireError(t, err, commonerrors.ErrInvalid)
errortest.RequireError(t, configTest.Validate(), commonerrors.ErrInvalid)

Expand Down
Loading