Skip to content

Commit 8f2cf97

Browse files
committed
refactor: substituted validations service's Validate function by CreateValidateFn
1 parent f5ff417 commit 8f2cf97

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

struct/mapper/validator/errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
var (
88
ErrNilService = errors.New("mapper validator service cannot be nil")
9+
ErrNilDestination = errors.New("destination cannot be nil")
10+
ErrDestinationNotPointer = errors.New("destination must be a pointer")
911
ErrNilMapper = errors.New("mapper cannot be nil")
1012
ErrNilValidator = errors.New("mapper validator cannot be nil")
1113
ErrFieldTagNameNotFound = "field tag name not found: %s"

struct/mapper/validator/service.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
govalidatormapper "github.com/ralvarezdev/go-validator/struct/mapper"
55
govalidatormapperparser "github.com/ralvarezdev/go-validator/struct/mapper/parser"
66
govalidatormappervalidation "github.com/ralvarezdev/go-validator/struct/mapper/validation"
7+
"reflect"
78
)
89

910
type (
@@ -17,11 +18,12 @@ type (
1718
interface{},
1819
error,
1920
)
20-
Validate(
21-
body interface{},
21+
CreateValidateFn(
2222
mapper *govalidatormapper.Mapper,
23-
validatorFns ...func(*govalidatormappervalidation.StructValidations) error,
24-
) func() (interface{}, error)
23+
validationsFns ...func(*govalidatormappervalidation.StructValidations) error,
24+
) func(
25+
dest interface{},
26+
) (interface{}, error)
2527
}
2628

2729
// DefaultService struct
@@ -78,19 +80,30 @@ func (d *DefaultService) ParseValidations(
7880
return parsedValidations, nil
7981
}
8082

81-
// Validate validates the request body using the validator functions provided
82-
// and returns the parsed validations. It validates the required fields by default
83-
func (d *DefaultService) Validate(
84-
body interface{},
83+
// CreateValidateFn creates a validate function for the request body using the validator
84+
// functions provided. It validates the required fields by default
85+
func (d *DefaultService) CreateValidateFn(
8586
mapper *govalidatormapper.Mapper,
86-
validatorFns ...func(*govalidatormappervalidation.StructValidations) error,
87-
) func() (interface{}, error) {
88-
return func() (
87+
validationsFns ...func(*govalidatormappervalidation.StructValidations) error,
88+
) func(
89+
dest interface{},
90+
) (interface{}, error) {
91+
return func(
92+
dest interface{},
93+
) (
8994
interface{},
9095
error,
9196
) {
97+
// Check if the destination is a pointer
98+
if dest == nil {
99+
return nil, ErrNilDestination
100+
}
101+
if reflect.TypeOf(dest).Kind() != reflect.Ptr {
102+
return nil, ErrDestinationNotPointer
103+
}
104+
92105
// Initialize struct fields validations from the request body
93-
rootStructValidations, err := govalidatormappervalidation.NewStructValidations(body)
106+
rootStructValidations, err := govalidatormappervalidation.NewStructValidations(dest)
94107
if err != nil {
95108
return nil, err
96109
}
@@ -104,7 +117,7 @@ func (d *DefaultService) Validate(
104117
}
105118

106119
// Run the validator functions
107-
for _, validatorFn := range validatorFns {
120+
for _, validatorFn := range validationsFns {
108121
if err = validatorFn(rootStructValidations); err != nil {
109122
return nil, err
110123
}

0 commit comments

Comments
 (0)