Skip to content

Commit bf80168

Browse files
dimacgkaInsei
authored andcommitted
added new option for transform error
1 parent 4e11790 commit bf80168

7 files changed

+50
-7
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/google/uuid v1.6.0
77
github.com/insei/fmap/v3 v3.1.1
88
github.com/stretchr/testify v1.9.0
9-
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
109
gopkg.in/yaml.v3 v3.0.1
1110
)
1211

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
88
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
99
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
1010
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
11-
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI=
12-
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
1311
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1412
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1513
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

helper.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
type helper struct {
1414
t translator.Translator
1515
getFieldLocation func(field fmap.Field) string
16+
transformError func(errs []shared.Error) []error
1617
}
1718

1819
// ErrorT returns a shared.Error with the given location, message, and value.

options.go

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package valigo
33
import (
44
"github.com/insei/fmap/v3"
55

6+
"github.com/insei/valigo/shared"
67
"github.com/insei/valigo/translator"
78
)
89

@@ -40,3 +41,10 @@ func WithFieldLocationNamingFn(fn func(field fmap.Field) string) Option {
4041
}
4142
})
4243
}
44+
45+
// WithErrorsTransformer returns an Option that sets the transformer for the Validator.
46+
func WithErrorsTransformer(fn func(errs []shared.Error) []error) Option {
47+
return optionFunc(func(v *Validator) {
48+
v.helper.transformError = fn
49+
})
50+
}

options_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package valigo
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/insei/fmap/v3"
78

9+
"github.com/insei/valigo/shared"
810
"github.com/insei/valigo/translator"
911
)
1012

@@ -78,3 +80,21 @@ func TestOptionApplyWithMultipleOptions(t *testing.T) {
7880
t.Errorf("expected getFieldLocation to be set")
7981
}
8082
}
83+
84+
func TestWithMultipleOptionsWithErrorsTransformer(t *testing.T) {
85+
transformer := func(errs []shared.Error) []error {
86+
var newErrs []error
87+
for _, err := range errs {
88+
newErrs = append(newErrs, fmt.Errorf("test: %v", err))
89+
}
90+
return newErrs
91+
}
92+
opt := WithErrorsTransformer(transformer)
93+
94+
v := New(opt)
95+
opt.apply(v)
96+
97+
if v.helper.transformError == nil {
98+
t.Errorf("expected transformError to be set")
99+
}
100+
}

validator.go

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ func (v *Validator) Validate(ctx context.Context, obj any) []error {
3636
for i, err := range errsTyped {
3737
errs[i] = err
3838
}
39+
if v.helper.transformError != nil {
40+
return v.helper.transformError(errsTyped)
41+
}
3942
return errs
4043
}
4144

validator_test.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package valigo
22

33
import (
44
"context"
5+
"fmt"
56
"reflect"
67
"regexp"
78
"testing"
@@ -150,10 +151,11 @@ func TestValidatorValidateTyped(t *testing.T) {
150151

151152
func TestValidatorValidate(t *testing.T) {
152153
tests := []struct {
153-
name string
154-
obj any
155-
validator func(ctx context.Context, h shared.Helper, obj any) []shared.Error
156-
expectedErrs int
154+
name string
155+
obj any
156+
validator func(ctx context.Context, h shared.Helper, obj any) []shared.Error
157+
transformError func(errs []shared.Error) []error
158+
expectedErrs int
157159
}{
158160
{
159161
name: "single error",
@@ -195,12 +197,24 @@ func TestValidatorValidate(t *testing.T) {
195197
},
196198
expectedErrs: 0,
197199
},
200+
{
201+
name: "transform error",
202+
obj: struct{}{},
203+
validator: func(ctx context.Context, h shared.Helper, obj any) []shared.Error {
204+
return []shared.Error{{Message: "test error"}}
205+
},
206+
transformError: func(errs []shared.Error) []error {
207+
return []error{fmt.Errorf("transformed error")}
208+
},
209+
expectedErrs: 1,
210+
},
198211
}
199212

200213
for _, test := range tests {
201214
t.Run(test.name, func(t *testing.T) {
202215
v := New()
203216
v.storage.validators[reflect.TypeOf(test.obj)] = []structValidationFn{test.validator}
217+
v.helper.transformError = test.transformError
204218
errs := v.Validate(context.Background(), test.obj)
205219
if len(errs) != test.expectedErrs {
206220
t.Errorf("expected %d errors, got %d", test.expectedErrs, len(errs))

0 commit comments

Comments
 (0)