support validate like revel.
Sure I'm a newbie in Go and Echo, but in my opinion it's bad idea because of Echo is just simple router we can validate request (form) using 3rd party package like https://github.com/asaskevich/govalidator or something like that.
package main
import (
"net/http"
"github.com/asaskevich/govalidator"
"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
)
type Form struct {
Name string `json:"name" valid:"optional,alphanum"`
Email string `json:"email "valid:"required,email"`
Card string `json:"card" valid:"required,creditcard"`
Code string `json:"code" valid:"numeric,length(3|4)"`
}
func main() {
router := echo.New()
router.POST("/", formHandler)
router.Run(standard.New(":8000"))
}
func formHandler(c echo.Context) error {
form := Form{}
if err := c.Bind(&form); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
_, err := govalidator.ValidateStruct(form)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
return c.String(http.StatusOK, "Ok")
}
Most helpful comment
Sure I'm a newbie in Go and Echo, but in my opinion it's bad idea because of Echo is just simple router we can validate request (form) using 3rd party package like https://github.com/asaskevich/govalidator or something like that.