4
4
govalidatormapper "github.com/ralvarezdev/go-validator/struct/mapper"
5
5
govalidatormapperparser "github.com/ralvarezdev/go-validator/struct/mapper/parser"
6
6
govalidatormappervalidation "github.com/ralvarezdev/go-validator/struct/mapper/validation"
7
+ "reflect"
7
8
)
8
9
9
10
type (
@@ -17,11 +18,12 @@ type (
17
18
interface {},
18
19
error ,
19
20
)
20
- Validate (
21
- body interface {},
21
+ CreateValidateFn (
22
22
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 )
25
27
}
26
28
27
29
// DefaultService struct
@@ -78,19 +80,30 @@ func (d *DefaultService) ParseValidations(
78
80
return parsedValidations , nil
79
81
}
80
82
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 (
85
86
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
+ ) (
89
94
interface {},
90
95
error ,
91
96
) {
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
+
92
105
// Initialize struct fields validations from the request body
93
- rootStructValidations , err := govalidatormappervalidation .NewStructValidations (body )
106
+ rootStructValidations , err := govalidatormappervalidation .NewStructValidations (dest )
94
107
if err != nil {
95
108
return nil , err
96
109
}
@@ -104,7 +117,7 @@ func (d *DefaultService) Validate(
104
117
}
105
118
106
119
// Run the validator functions
107
- for _ , validatorFn := range validatorFns {
120
+ for _ , validatorFn := range validationsFns {
108
121
if err = validatorFn (rootStructValidations ); err != nil {
109
122
return nil , err
110
123
}
0 commit comments