Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve formatting of error message
Browse files Browse the repository at this point in the history
ga-paul-t committed Feb 1, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 28828ff commit f0f640f
Showing 2 changed files with 24 additions and 8 deletions.
30 changes: 23 additions & 7 deletions internal/config/uplift.go
Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@ type JSONBump struct {

// CommitAuthor defines configuration about the author of a git commit
type CommitAuthor struct {
Name string `yaml:"name" validate:"required_without=Email,min=1"`
Name string `yaml:"name" validate:"required_without=Email,omitempty,min=1"`
Email string `yaml:"email" validate:"required_without=Name,email"`
}

@@ -186,12 +186,28 @@ func (c Uplift) Validate() error {
errMsg.WriteString("uplift configuration contains validation errors. Please fix before proceeding:\n")

for _, err := range err.(validator.ValidationErrors) {
valMsg := fmt.Sprintf("field '%s' at '%s' contains unexpected value '%v'\n",
err.Field(),
err.Namespace(),
err.Value())

errMsg.WriteString(valMsg)
var reason string
switch err.ActualTag() {
case "min":
reason = fmt.Sprintf("contains a value that does not meet the minimum expected length of '%s'\n", err.Param())
case "file":
reason = fmt.Sprintf("contains a path to a file that does not exist '%v'\n", err.Value())
case "url":
reason = fmt.Sprintf("contains an invalid url '%s'\n", err.Value())
case "email":
reason = fmt.Sprintf("contains an invalid email address '%s'\n", err.Value())
case "oneof":
reason = fmt.Sprintf("contains a value that is not one of the following [%s]\n", err.Param())
case "required_without":
reason = fmt.Sprintf("must be provided when field '%s' is missing\n", err.Param())
case "required_without_all":
reason = fmt.Sprintf("must be provided when all other fields [%s] are missing\n", err.Param())
default:
reason = fmt.Sprintf("contains an unexpected value '%v'\n", err.Value())
}

errMsg.WriteString(fmt.Sprintf("field '%s' at '%s' ", err.Field(), err.Namespace()))
errMsg.WriteString(reason)
}

return errors.New(errMsg.String())
2 changes: 1 addition & 1 deletion internal/config/uplift_test.go
Original file line number Diff line number Diff line change
@@ -251,7 +251,7 @@ func TestValidateCommitEmail(t *testing.T) {
}

err := cfg.Validate()
require.ErrorContains(t, err, "Uplift.CommitAuthor.Name")
require.ErrorContains(t, err, "Uplift.CommitAuthor.Email")
}

func TestValidateChangelogSort(t *testing.T) {

0 comments on commit f0f640f

Please sign in to comment.