Skip to content

Commit 9409d8f

Browse files
dimacgkaInsei
authored andcommitted
changed transformError
1 parent bf80168 commit 9409d8f

File tree

6 files changed

+42
-20
lines changed

6 files changed

+42
-20
lines changed

examples/custom/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func main() {
2121
// Custom on struct type
2222
c.Custom(func(ctx context.Context, h shared.StructCustomHelper, obj *User) []shared.Error {
2323
if obj.Name != "Rebecca" {
24-
format := "Only Rebecca name is allowed" // you can add translations if you want, see translations example
24+
format := "Only Rebecca name is allowed" // you can add translations if you want, see translation example
2525
return []shared.Error{h.ErrorT(ctx, &obj.Name, obj.Name, format)}
2626
}
2727
return nil
@@ -31,7 +31,7 @@ func main() {
3131
c.String(&obj.LastName).
3232
Custom(func(ctx context.Context, h *shared.FieldCustomHelper, value *string) []shared.Error {
3333
if *value != "Rebecca" {
34-
localeKey := "Only \"Rebecca\" is allowed" // you can add translations if you want, see translations example
34+
localeKey := "Only Rebecca is allowed" // you can add translations if you want, see translation example
3535
return []shared.Error{h.ErrorT(ctx, value, localeKey)}
3636
}
3737
return nil

helper.go

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

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

options.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func WithFieldLocationNamingFn(fn func(field fmap.Field) string) Option {
4545
// WithErrorsTransformer returns an Option that sets the transformer for the Validator.
4646
func WithErrorsTransformer(fn func(errs []shared.Error) []error) Option {
4747
return optionFunc(func(v *Validator) {
48-
v.helper.transformError = fn
48+
if fn != nil {
49+
v.transformError = fn
50+
}
4951
})
5052
}

options_test.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,30 @@ func TestWithMultipleOptionsWithErrorsTransformer(t *testing.T) {
8989
}
9090
return newErrs
9191
}
92-
opt := WithErrorsTransformer(transformer)
9392

94-
v := New(opt)
95-
opt.apply(v)
93+
testCases := []struct {
94+
name string
95+
transformer func([]shared.Error) []error
96+
}{
97+
{
98+
name: "Transformer set",
99+
transformer: transformer,
100+
},
101+
{
102+
name: "Transformer not set",
103+
transformer: nil,
104+
},
105+
}
106+
107+
for _, tc := range testCases {
108+
t.Run(tc.name, func(t *testing.T) {
109+
opt := WithErrorsTransformer(tc.transformer)
110+
v := New(opt)
111+
opt.apply(v)
96112

97-
if v.helper.transformError == nil {
98-
t.Errorf("expected transformError to be set")
113+
if v.transformError == nil {
114+
t.Errorf("expected function, but got nil")
115+
}
116+
})
99117
}
100118
}

validator.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99

1010
// Validator is a struct that holds a storage and a helper object.
1111
type Validator struct {
12-
storage *storage
13-
helper *helper
12+
storage *storage
13+
helper *helper
14+
transformError func(errs []shared.Error) []error
1415
}
1516

1617
// ValidateTyped validates an object of any type using validators from the storage.
@@ -32,14 +33,7 @@ func (v *Validator) ValidateTyped(ctx context.Context, obj any) []shared.Error {
3233
// objects instead of shared.Error objects.
3334
func (v *Validator) Validate(ctx context.Context, obj any) []error {
3435
errsTyped := v.ValidateTyped(ctx, obj)
35-
errs := make([]error, len(errsTyped))
36-
for i, err := range errsTyped {
37-
errs[i] = err
38-
}
39-
if v.helper.transformError != nil {
40-
return v.helper.transformError(errsTyped)
41-
}
42-
return errs
36+
return v.transformError(errsTyped)
4337
}
4438

4539
// GetHelper returns the helper object associated with the Validator instance.
@@ -53,6 +47,13 @@ func New(opts ...Option) *Validator {
5347
v := &Validator{
5448
storage: newStorage(),
5549
helper: newHelper(),
50+
transformError: func(errs []shared.Error) []error {
51+
errsNew := make([]error, len(errs))
52+
for i, err := range errs {
53+
errsNew[i] = err
54+
}
55+
return errsNew
56+
},
5657
}
5758
for _, opt := range opts {
5859
opt.apply(v)

validator_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ func TestValidatorValidate(t *testing.T) {
214214
t.Run(test.name, func(t *testing.T) {
215215
v := New()
216216
v.storage.validators[reflect.TypeOf(test.obj)] = []structValidationFn{test.validator}
217-
v.helper.transformError = test.transformError
217+
if test.transformError != nil {
218+
v.transformError = test.transformError
219+
}
218220
errs := v.Validate(context.Background(), test.obj)
219221
if len(errs) != test.expectedErrs {
220222
t.Errorf("expected %d errors, got %d", test.expectedErrs, len(errs))

0 commit comments

Comments
 (0)