Skip to content
Myr

Export struct sliceValidateError to allow error casting and rename it as (#2777)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

SliceValidationError

authored by edebernis · committed by GitHub · parent 6aee456 · Browse files · 3 files+14 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Emeric de Bernis <emeric.debernis@adevinta.com>

Diff stats

3 files+14 added−14 removed

  • other:+14 added−14 removed

3 files+14 added−14 removed

modified:binding/default_validator_benchmark_test.go+2 added-2 removed
4 unmodified lines
5 "strconv"6 "testing"7)89func BenchmarkSliceValidateError(b *testing.B) {9func BenchmarkSliceValidationError(b *testing.B) {10 const size int = 10011 for i := 0; i < b.N; i++ {12 e := make(sliceValidateError, size)12 e := make(SliceValidationError, size)13 for j := 0; j < size; j++ {14 e[j] = errors.New(strconv.Itoa(j))15 }16 if len(e.Error()) == 0 {
4 unmodified lines
modified:binding/default_validator_test.go+8 added-8 removed
7 unmodified lines
8 "errors"9 "testing"10)1112func TestSliceValidateError(t *testing.T) {12func TestSliceValidationError(t *testing.T) {13 tests := []struct {14 name string15 err sliceValidateError15 err SliceValidationError16 want string17 }{18 {"has nil elements", sliceValidateError{errors.New("test error"), nil}, "[0]: test error"},19 {"has zero elements", sliceValidateError{}, ""},20 {"has one element", sliceValidateError{errors.New("test one error")}, "[0]: test one error"},18 {"has nil elements", SliceValidationError{errors.New("test error"), nil}, "[0]: test error"},19 {"has zero elements", SliceValidationError{}, ""},20 {"has one element", SliceValidationError{errors.New("test one error")}, "[0]: test one error"},21 {"has two elements",22 sliceValidateError{22 SliceValidationError{23 errors.New("first error"),24 errors.New("second error"),25 },26 "[0]: first error\n[1]: second error",27 },28 {"has many elements",29 sliceValidateError{29 SliceValidationError{30 errors.New("first error"),31 errors.New("second error"),32 nil,33 nil,
5 unmodified lines
39 }40 for _, tt := range tests {41 t.Run(tt.name, func(t *testing.T) {42 if got := tt.err.Error(); got != tt.want {43 t.Errorf("sliceValidateError.Error() = %v, want %v", got, tt.want)43 t.Errorf("SliceValidationError.Error() = %v, want %v", got, tt.want)44 }45 })46 }47}
41 unmodified lines
modified:binding/default_validator.go+4 added-4 removed
16 unmodified lines
17 once sync.Once18 validate *validator.Validate19}2021type sliceValidateError []error21type SliceValidationError []error2223// Error concatenates all error elements in sliceValidateError into a single string separated by \n.24func (err sliceValidateError) Error() string {23// Error concatenates all error elements in SliceValidationError into a single string separated by \n.24func (err SliceValidationError) Error() string {25 n := len(err)26 switch n {27 case 0:28 return ""
29 unmodified lines
58 case reflect.Struct:59 return v.validateStruct(obj)60 case reflect.Slice, reflect.Array:61 count := value.Len()62 validateRet := make(sliceValidateError, 0)62 validateRet := make(SliceValidationError, 0)63 for i := 0; i < count; i++ {64 if err := v.ValidateStruct(value.Index(i).Interface()); err != nil {65 validateRet = append(validateRet, err)66 }
31 unmodified lines