Skip to content

Commit

Permalink
changed transformError
Browse files Browse the repository at this point in the history
  • Loading branch information
dimacgka committed Dec 3, 2024
1 parent 770e118 commit bdd96bc
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
4 changes: 2 additions & 2 deletions examples/custom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
// Custom on struct type
c.Custom(func(ctx context.Context, h shared.StructCustomHelper, obj *User) []shared.Error {
if obj.Name != "Rebecca" {
format := "Only Rebecca name is allowed" // you can add translations if you want, see translations example
format := "Only Rebecca name is allowed" // you can add translations if you want, see translation example
return []shared.Error{h.ErrorT(ctx, &obj.Name, obj.Name, format)}
}
return nil
Expand All @@ -31,7 +31,7 @@ func main() {
c.String(&obj.LastName).
Custom(func(ctx context.Context, h *shared.FieldCustomHelper, value *string) []shared.Error {
if *value != "Rebecca" {
localeKey := "Only \"Rebecca\" is allowed" // you can add translations if you want, see translations example
localeKey := "Only Rebecca is allowed" // you can add translations if you want, see translation example
return []shared.Error{h.ErrorT(ctx, value, localeKey)}
}
return nil
Expand Down
1 change: 0 additions & 1 deletion helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
type helper struct {
t translator.Translator
getFieldLocation func(field fmap.Field) string
transformError func(errs []shared.Error) []error
}

// ErrorT returns a shared.Error with the given location, message, and value.
Expand Down
4 changes: 3 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func WithFieldLocationNamingFn(fn func(field fmap.Field) string) Option {
// WithErrorsTransformer returns an Option that sets the transformer for the Validator.
func WithErrorsTransformer(fn func(errs []shared.Error) []error) Option {
return optionFunc(func(v *Validator) {
v.helper.transformError = fn
if fn != nil {
v.transformError = fn
}
})
}
11 changes: 10 additions & 1 deletion options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,16 @@ func TestWithMultipleOptionsWithErrorsTransformer(t *testing.T) {
v := New(opt)
opt.apply(v)

if v.helper.transformError == nil {
if v.transformError == nil {
t.Errorf("expected transformError to be set")
}

opt2 := WithErrorsTransformer(nil)

v2 := New(opt2)
opt2.apply(v2)

if v2.transformError == nil {
t.Errorf("expected transformError to be set")
}
}
21 changes: 11 additions & 10 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

// Validator is a struct that holds a storage and a helper object.
type Validator struct {
storage *storage
helper *helper
storage *storage
helper *helper
transformError func(errs []shared.Error) []error
}

// ValidateTyped validates an object of any type using validators from the storage.
Expand All @@ -32,14 +33,7 @@ func (v *Validator) ValidateTyped(ctx context.Context, obj any) []shared.Error {
// objects instead of shared.Error objects.
func (v *Validator) Validate(ctx context.Context, obj any) []error {
errsTyped := v.ValidateTyped(ctx, obj)
errs := make([]error, len(errsTyped))
for i, err := range errsTyped {
errs[i] = err
}
if v.helper.transformError != nil {
return v.helper.transformError(errsTyped)
}
return errs
return v.transformError(errsTyped)
}

// GetHelper returns the helper object associated with the Validator instance.
Expand All @@ -53,6 +47,13 @@ func New(opts ...Option) *Validator {
v := &Validator{
storage: newStorage(),
helper: newHelper(),
transformError: func(errs []shared.Error) []error {
errsNew := make([]error, len(errs))
for i, err := range errs {
errsNew[i] = err
}
return errsNew
},
}
for _, opt := range opts {
opt.apply(v)
Expand Down
4 changes: 3 additions & 1 deletion validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ func TestValidatorValidate(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
v := New()
v.storage.validators[reflect.TypeOf(test.obj)] = []structValidationFn{test.validator}
v.helper.transformError = test.transformError
if test.transformError != nil {
v.transformError = test.transformError
}
errs := v.Validate(context.Background(), test.obj)
if len(errs) != test.expectedErrs {
t.Errorf("expected %d errors, got %d", test.expectedErrs, len(errs))
Expand Down

0 comments on commit bdd96bc

Please sign in to comment.