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

Fix validation errors builder string #109

Merged
merged 4 commits into from
Mar 19, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
vendor
gomock_generator/gomock_generator

# IDE
.idea/
18 changes: 11 additions & 7 deletions errors/validation_errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package errors

import (
"bytes"
"fmt"
"strings"
)

Expand All @@ -11,14 +11,18 @@ type ValidationErrors struct {
}

func (v *ValidationErrors) Error() string {
var buffer bytes.Buffer
var builder strings.Builder
index := 0

for field, errors := range v.Errors {
buffer.WriteString(field)
buffer.WriteString("=")
buffer.WriteString(strings.Join(errors, ","))
for field, errs := range v.Errors {
index++
builder.WriteString(fmt.Sprintf("%s=%s", field, strings.Join(errs, ", ")))
if index < len(v.Errors) {
builder.WriteString(" ")
}
}
return buffer.String()

return builder.String()
}

// ValidationErrorsBuilder is used to provide a simple way to create a ValidationErrors struct. The typical usecase is:
Expand Down
46 changes: 44 additions & 2 deletions errors/validation_errors_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package errors

import "github.com/stretchr/testify/require"
import "testing"
import (
"testing"

"github.com/stretchr/testify/require"
)

func TestValidationErrorsBuilder_Merge(t *testing.T) {
cases := map[string]struct {
Expand Down Expand Up @@ -60,3 +63,42 @@ func TestValidationErrorsBuilder_MergeWithPrefix(t *testing.T) {
})
}
}

func TestValidationErrors_Error(t *testing.T) {
cases := map[string]struct {
Errors ValidationErrors
ExpectedString string
}{
"should return a string with one error in it": {
Errors: ValidationErrors{
Errors: map[string][]string{
"name": {"invalid name"},
},
},
ExpectedString: "name=invalid name",
},
"should return a string with multiple errors in it with the same field name": {
Errors: ValidationErrors{
Errors: map[string][]string{
"name": {"invalid name", "should contains alphanumeric characters"},
},
},
ExpectedString: "name=invalid name, should contains alphanumeric characters",
},
"should return a string with multiple errors in it with multiple field name": {
Errors: ValidationErrors{
Errors: map[string][]string{
"name": {"invalid name", "should contains alphanumeric characters"},
"type": {"invalid type", "type not exist"},
},
},
ExpectedString: "name=invalid name, should contains alphanumeric characters type=invalid type, type not exist",
},
}

for title, c := range cases {
t.Run(title, func(t *testing.T) {
require.Equal(t, c.ExpectedString, c.Errors.Error())
})
}
}