From 44aa551f6272fe876ff7f75b12f2ff31f71b689a Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Sun, 3 Sep 2023 19:39:44 +0200 Subject: [PATCH] Update required error message --- aconfig.go | 6 ++---- aconfig_test.go | 9 +++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/aconfig.go b/aconfig.go index e9f36e9..2baf014 100644 --- a/aconfig.go +++ b/aconfig.go @@ -270,13 +270,11 @@ func (l *Loader) checkRequired() error { missedFields = append(missedFields, field.name) } } + if len(missedFields) == 0 { return nil } - if len(missedFields) == 1 { - return fmt.Errorf("field %s is required but not set", missedFields[0]) - } - return fmt.Errorf("fields %s are required but not set", strings.Join(missedFields, ",")) + return fmt.Errorf("fields required but not set: %s", strings.Join(missedFields, ",")) } func (l *Loader) loadDefaults() error { diff --git a/aconfig_test.go b/aconfig_test.go index 33dc53f..48a5815 100644 --- a/aconfig_test.go +++ b/aconfig_test.go @@ -1096,12 +1096,13 @@ func TestMissingFieldWithRequiredTag(t *testing.T) { }) err := loader.Load() + want := "load config: fields required but not set: Field1" - want := "load config: field Field1 is required but not set" - if err.Error() != want { + if have := err.Error(); have != want { t.Fatalf("got %v, want %v", err, want) } } + func TestMissingFieldsWithRequiredTag(t *testing.T) { cfg := struct { Field1 string `required:"true"` @@ -1112,9 +1113,9 @@ func TestMissingFieldsWithRequiredTag(t *testing.T) { }) err := loader.Load() + want := "load config: fields required but not set: Field1,Field2" - want := "load config: fields Field1,Field2 are required but not set" - if err.Error() != want { + if have := err.Error(); have != want { t.Fatalf("got %v, want %v", err, want) } }