Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update required error message #144

Merged
merged 1 commit into from Sep 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions aconfig.go
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
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