Skip to content

Commit

Permalink
Update required error message (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Sep 3, 2023
1 parent 3b3b803 commit a54d561
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
6 changes: 2 additions & 4 deletions aconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions aconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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)
}
}
Expand Down

0 comments on commit a54d561

Please sign in to comment.