Skip to content

Commit

Permalink
Export struct sliceValidateError to allow error casting and rename it…
Browse files Browse the repository at this point in the history
… as (gin-gonic#2777)

SliceValidationError

Co-authored-by: Emeric de Bernis <emeric.debernis@adevinta.com>
  • Loading branch information
2 people authored and daheige committed Apr 18, 2022
1 parent 27eb0a2 commit 3fbf7bb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions binding/default_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ type defaultValidator struct {
validate *validator.Validate
}

type sliceValidateError []error
type SliceValidationError []error

// Error concatenates all error elements in sliceValidateError into a single string separated by \n.
func (err sliceValidateError) Error() string {
// Error concatenates all error elements in SliceValidationError into a single string separated by \n.
func (err SliceValidationError) Error() string {
n := len(err)
switch n {
case 0:
Expand Down Expand Up @@ -59,7 +59,7 @@ func (v *defaultValidator) ValidateStruct(obj interface{}) error {
return v.validateStruct(obj)
case reflect.Slice, reflect.Array:
count := value.Len()
validateRet := make(sliceValidateError, 0)
validateRet := make(SliceValidationError, 0)
for i := 0; i < count; i++ {
if err := v.ValidateStruct(value.Index(i).Interface()); err != nil {
validateRet = append(validateRet, err)
Expand Down
4 changes: 2 additions & 2 deletions binding/default_validator_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"testing"
)

func BenchmarkSliceValidateError(b *testing.B) {
func BenchmarkSliceValidationError(b *testing.B) {
const size int = 100
for i := 0; i < b.N; i++ {
e := make(sliceValidateError, size)
e := make(SliceValidationError, size)
for j := 0; j < size; j++ {
e[j] = errors.New(strconv.Itoa(j))
}
Expand Down
16 changes: 8 additions & 8 deletions binding/default_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ import (
"testing"
)

func TestSliceValidateError(t *testing.T) {
func TestSliceValidationError(t *testing.T) {
tests := []struct {
name string
err sliceValidateError
err SliceValidationError
want string
}{
{"has nil elements", sliceValidateError{errors.New("test error"), nil}, "[0]: test error"},
{"has zero elements", sliceValidateError{}, ""},
{"has one element", sliceValidateError{errors.New("test one error")}, "[0]: test one error"},
{"has nil elements", SliceValidationError{errors.New("test error"), nil}, "[0]: test error"},
{"has zero elements", SliceValidationError{}, ""},
{"has one element", SliceValidationError{errors.New("test one error")}, "[0]: test one error"},
{"has two elements",
sliceValidateError{
SliceValidationError{
errors.New("first error"),
errors.New("second error"),
},
"[0]: first error\n[1]: second error",
},
{"has many elements",
sliceValidateError{
SliceValidationError{
errors.New("first error"),
errors.New("second error"),
nil,
Expand All @@ -40,7 +40,7 @@ func TestSliceValidateError(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.err.Error(); got != tt.want {
t.Errorf("sliceValidateError.Error() = %v, want %v", got, tt.want)
t.Errorf("SliceValidationError.Error() = %v, want %v", got, tt.want)
}
})
}
Expand Down

0 comments on commit 3fbf7bb

Please sign in to comment.