-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Closed
Description
Description
I used example code with validate and it's return me some errors.
Checklist
- Dependencies installed
- No typos
- Searched existing issues and docs
Expected behaviour
Should validate data with 3rd party suck as https://github.com/go-playground/validator
Actual behaviour
>> go run server.go
# command-line-arguments
./server.go:27: e.Validator undefined (type *echo.Echo has no field or method Validator)
./server.go:33: c.Validate undefined (type echo.Context has no field or method Validate)
Steps to reproduce
run the code below
Working code to debug
package main
import (
"net/http"
"github.com/labstack/echo"
"gopkg.in/go-playground/validator.v9"
)
type (
User struct {
Name string `json:"name" validate:"required"`
Email string `json:"email" validate:"required,email"`
}
CustomValidator struct {
validator *validator.Validate
}
)
func (cv *CustomValidator) Validate(i interface{}) error {
return cv.validator.Struct(i)
}
func main() {
e := echo.New()
e.Validator = &CustomValidator{validator: validator.New()}
e.POST("/users", func(c echo.Context) (err error) {
u := new(User)
if err = c.Bind(u); err != nil {
return
}
if err = c.Validate(u); err != nil {
return
}
return c.JSON(http.StatusOK, u)
})
e.Logger.Fatal(e.Start(":1323"))
}
Version/commit
go1.7.3 darwin/amd64
Metadata
Metadata
Assignees
Labels
No labels