Skip to content

Commit ceb3129

Browse files
committed
refactor: dynamic validation of the function and its parameters now it's with anticipation
1 parent 03799a5 commit ceb3129

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/ralvarezdev/go-flags v0.3.1
77
github.com/ralvarezdev/go-jwt v0.4.8
88
github.com/ralvarezdev/go-logger v0.4.5
9-
github.com/ralvarezdev/go-reflect v0.2.1
9+
github.com/ralvarezdev/go-reflect v0.2.3
1010
github.com/ralvarezdev/go-validator v0.5.22
1111
)
1212

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ github.com/ralvarezdev/go-jwt v0.4.8 h1:ugZ0Lyfen7QzmgweLnJynya3yih8rPev48Xjj7HY
66
github.com/ralvarezdev/go-jwt v0.4.8/go.mod h1:RGGbn72uT5av5Hlo50H4xSXg6chmnpecl/a2yGAFNzQ=
77
github.com/ralvarezdev/go-logger v0.4.5 h1:Lw7DdZUb4YSQDA7J9nv79soojw1eFHVVzO8wdE9d+Qg=
88
github.com/ralvarezdev/go-logger v0.4.5/go.mod h1:vq47onxpED19/o3dh5/YbBKK0eZhqJENPgACYkqNNi8=
9-
github.com/ralvarezdev/go-reflect v0.2.1 h1:Q200FruGvc8INRRpnmvK+iRKTnCslRAWnwGAj1fXMak=
10-
github.com/ralvarezdev/go-reflect v0.2.1/go.mod h1:jv3EqQfUixWtNeJd/Rnzf0IDcYZ1z3GApRdAGQHdHZQ=
9+
github.com/ralvarezdev/go-reflect v0.2.3 h1:WXtgMbZ1LFKjyzi2W3JetgQCJcT/N2erDv12ZZaaLBI=
10+
github.com/ralvarezdev/go-reflect v0.2.3/go.mod h1:jv3EqQfUixWtNeJd/Rnzf0IDcYZ1z3GApRdAGQHdHZQ=
1111
github.com/ralvarezdev/go-strings v0.1.6 h1:NWI+XCipWaOo0vhSNpZz+7Eb4igk/GXD5RaFsk4ZPqY=
1212
github.com/ralvarezdev/go-strings v0.1.6/go.mod h1:8sFOqmPJpqzS7bTjf91EzUCITnwpmkfifwY80GxV5r8=
1313
github.com/ralvarezdev/go-validator v0.5.22 h1:mjvWKwhQyqQ6FwJUWIPfsywjOK0nMYf7HKDYAojh1xo=

http/middleware/validator/errors.go

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

77
var (
8-
ErrNilValidateFn = errors.New("validate function is nil")
9-
ErrInvalidValidateFn = errors.New("validate function is invalid")
8+
ErrNilValidateFn = errors.New("validate function is nil")
9+
ErrInvalidValidateFn = errors.New("validate function is invalid")
10+
ErrNilCreateValidateFn = errors.New("create validate function is nil")
11+
ErrNilParametersValues = errors.New("parameters value is nil")
1012
)

http/middleware/validator/middleware.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func (m *Middleware) Validate(
4545
// Dereference the body type if it is a pointer
4646
if bodyType.Kind() == reflect.Pointer {
4747
bodyType = bodyType.Elem()
48+
} else {
49+
body = &body
4850
}
4951

5052
// Create the mapper
@@ -53,17 +55,37 @@ func (m *Middleware) Validate(
5355
panic(err)
5456
}
5557

58+
// Check if the createValidateFn is valid
59+
if createValidateFn == nil {
60+
panic(ErrNilCreateValidateFn)
61+
}
62+
63+
// Check if the type of the createValidateFn is a function and the parameters are correct
64+
fnValuePtr, paramsValuesPtr, err := goreflect.CheckFunction(
65+
createValidateFn,
66+
body,
67+
mapper,
68+
)
69+
if err != nil {
70+
panic(err)
71+
}
72+
73+
// Check if the parameters values are not nil
74+
if paramsValuesPtr == nil {
75+
panic(ErrNilParametersValues)
76+
}
77+
paramsValues := *paramsValuesPtr
78+
5679
return func(next http.Handler) http.Handler {
5780
return http.HandlerFunc(
5881
func(w http.ResponseWriter, r *http.Request) {
5982
// Get a new instance of the body
6083
dest := goreflect.NewInstanceFromType(bodyType)
6184

62-
// Get the validate function
63-
results, err := goreflect.CallFunction(
64-
createValidateFn,
65-
dest,
66-
mapper,
85+
// Call the validate function
86+
results, err := goreflect.UnsafeCallFunction(
87+
fnValuePtr,
88+
paramsValues...,
6789
)
6890
if err != nil {
6991
panic(err)

0 commit comments

Comments
 (0)