@@ -91,7 +91,15 @@ func DefaultDeepConfiguration() *DeepConfig {
9191}
9292
9393func (cfg * DeepConfig ) Validate () error {
94- return nil
94+ // Validate Embedded Structs
95+ err := ValidateEmbedded (cfg )
96+ if err != nil {
97+ return err
98+ }
99+
100+ return validation .ValidateStruct (cfg ,
101+ validation .Field (& cfg .TestConfigDeep , validation .Required ),
102+ )
95103}
96104
97105func (cfg * ConfigurationTest ) Validate () error {
@@ -125,8 +133,37 @@ func TestErrorFormatting(t *testing.T) {
125133 cfg := DefaultConfiguration ()
126134 err := cfg .Validate ()
127135 require .Error (t , err )
136+
137+ errortest .AssertError (t , err , commonerrors .ErrInvalid )
138+ assert .Contains (t , err .Error (), "invalid: structure failed validation: (TestConfig->db) [DUMMYCONFIG_DB] cannot be blank" )
139+ }
140+
141+ func TestDeepErrorFormatting (t * testing.T ) {
142+ defaults := DefaultDeepConfiguration ()
143+ err := defaults .Validate ()
144+ require .Error (t , err )
145+
128146 errortest .AssertError (t , err , commonerrors .ErrInvalid )
129- assert .Contains (t , err .Error (), "invalid: structure failed validation: (TestConfig->db) [DUMMYCONFIG] cannot be blank" )
147+ assert .Contains (t , err .Error (), "invalid: structure failed validation: (TestConfigDeep->TestConfig->db) [DEEP_CONFIG_DUMMYCONFIG_DB] cannot be blank" )
148+
149+ err = os .Setenv ("TEST_DEEP_CONFIG_DUMMYCONFIG_DB" , "a test db" )
150+ require .NoError (t , err )
151+ err = os .Setenv ("TEST_DEEP_CONFIG_DUMMYCONFIG_DUMMY_HOST" , "a test host" )
152+ require .NoError (t , err )
153+ err = os .Setenv ("TEST_DEEP_CONFIG_DUMMYCONFIG_PASSWORD" , "a test password" )
154+ require .NoError (t , err )
155+ err = os .Setenv ("TEST_DEEP_CONFIG_DUMMYCONFIG_USER" , "a test user" )
156+ require .NoError (t , err )
157+ err = os .Setenv ("TEST_DEEP_CONFIG_DUMMY_CONFIG_DB" , "a test user" )
158+ require .NoError (t , err )
159+
160+ t .Run ("defined mapstructure" , func (t * testing.T ) {
161+ configTest2 := & DeepConfig {}
162+ err = LoadFromSystem ("test" , configTest2 , defaults )
163+
164+ errortest .AssertError (t , err , commonerrors .ErrInvalid )
165+ assert .Contains (t , err .Error (), "invalid: structure failed validation: (TestConfigDeep->TestConfig2->dummy_host) [TEST_DEEP_CONFIG_DUMMY_CONFIG_DUMMY_HOST] cannot be blank" )
166+ })
130167}
131168
132169func TestServiceConfigurationLoad (t * testing.T ) {
@@ -137,6 +174,9 @@ func TestServiceConfigurationLoad(t *testing.T) {
137174 err := Load ("test" , configTest , defaults )
138175 // Some required values are missing.
139176 require .Error (t , err )
177+
178+ assert .ErrorContains (t , err , "(TestConfig->db) [TEST_DUMMYCONFIG_DB] cannot be blank" )
179+
140180 errortest .RequireError (t , err , commonerrors .ErrInvalid )
141181 errortest .RequireError (t , configTest .Validate (), commonerrors .ErrInvalid )
142182
0 commit comments