I am trying to create validator/binder middleware in go using gin framework.
This is the model
type LoginForm struct{
Email string `json:"email" form:"email" binding:"email,required"`
Password string `json:"password" form:"password" binding:"required"`
}
Router
router.POST("/login",middlewares.Validator(LoginForm{}) ,controllers.Login)
Middleware
func Validator(v interface{}) gin.HandlerFunc{
return func(c *gin.Context){
a := reflect.New(reflect.TypeOf(v))
err:=c.Bind(&a)
if(err!=nil){
respondWithError(401, "Login Error", c)
return
}
c.Set("LoginForm",a)
c.Next()
}
}
I am very new to golang. I understand the problem is with the binding to the wrong variable. Is there any other way of solving this?