-
I want to validate the I can validate a Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
What you want to do is to:
Path parameter can be bind to struct with so you need to add similar field to your bindable struct If you do not want to use struct then you need to first convert path param to int and then use var id int64
if err = echo.PathParamsBinder(c).Int64("id", &id).BindError(); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if err = validator.New().Var(id, "gt=1,lt=10"); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
} Working example for validating single variable: var validate = validator.New()
func main() {
e := echo.New()
e.POST("/users/:id", func(c echo.Context) error {
var id int64
if err := echo.PathParamsBinder(c).Int64("id", &id).BindError(); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if err := validate.Var(id, "gt=1,lt=10"); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusOK, map[string]string{"status": "ok"})
})
e.Logger.Fatal(e.Start(":1323"))
} Working example for struct: type (
User struct {
ID int64 `param:"id" validate:"required,gt=1,lt=10"`
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 {
if err := cv.validator.Struct(i); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return nil
}
func main() {
e := echo.New()
e.Validator = &CustomValidator{validator: validator.New()}
e.POST("/users/:id", func(c echo.Context) (err error) {
u := new(User)
if err = c.Bind(u); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if err = c.Validate(u); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusOK, u)
})
e.Logger.Fatal(e.Start(":1323"))
} Example request: x@x~/code$ curl -v -X POST http://localhost:1323/users/2 -H 'Content-Type: application/json' -d '{"name":"Joe","email":"[email protected]"}'
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 127.0.0.1:1323...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 1323 (#0)
> POST /users/2 HTTP/1.1
> Host: localhost:1323
> User-Agent: curl/7.68.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 37
>
* upload completely sent off: 37 out of 37 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=UTF-8
< Date: Thu, 25 Mar 2021 20:38:19 GMT
< Content-Length: 45
<
{"ID":2,"name":"Joe","email":"[email protected]"}
* Connection #0 to host localhost left intact and example of validation error: x@x:~/code$ curl -v -X POST http://localhost:1323/users/11 -H 'Content-Type: application/json' -d '{"name":"Joe","email":"[email protected]"}'
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 127.0.0.1:1323...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 1323 (#0)
> POST /users/11 HTTP/1.1
> Host: localhost:1323
> User-Agent: curl/7.68.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 37
>
* upload completely sent off: 37 out of 37 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Bad Request
< Content-Type: application/json; charset=UTF-8
< Date: Thu, 25 Mar 2021 20:46:08 GMT
< Content-Length: 102
<
{"message":"code=500, message=Key: 'User.ID' Error:Field validation for 'ID' failed on the 'lt' tag"}
* Connection #0 to host localhost left intact |
Beta Was this translation helpful? Give feedback.
What you want to do is to:
Path parameter can be bind to struct with
param="id"
tagValidation can be added with field tag
validate:"gt=1,lt=10"
so you need to add similar field to your bindable struct
ID int64 `param:"id" validate:"required,gt=1,lt=10"`
If you do not want to use struct then you need to first convert path param to int and then use
validator.Var
on it.