diff --git a/bind_readme.md b/bind_readme.md index 88883cdf368..77cc5773bca 100644 --- a/bind_readme.md +++ b/bind_readme.md @@ -58,7 +58,7 @@ func main() { app.Get("/:id", func(c fiber.Ctx) error { var req Req - if err := c.Bind(&req); err != nil { + if err := c.Bind().Req(&req).Err(); err != nil { return err } return c.JSON(req) @@ -98,77 +98,75 @@ unmarshal raw string we get from request's query/header/... You don't need to set a field tag and it's binding tag will be ignored. -```golang +``` type Binder interface { -UnmarshalFiberCtx(ctx fiber.Ctx) error + UnmarshalFiberCtx(ctx fiber.Ctx) error } ``` If your type implement `fiber.Binder`, bind will pass current request Context to your and you can unmarshal the info you need. -### Bind With validation - -Normally, `bind` will only try to unmarshal data from request and pass it to request handler. +### Parse Request Body -you can call `ctx.BindWithValidate()` to bind and validate your request data. +you can call `ctx.BodyJSON(v any) error` or `BodyXML(v any) error` -And you will need to set a validator in app Config, otherwise it will always return an error. +These methods will check content-type HTTP header and call configured JSON or XML decoder to unmarshal. -```go +```golang package main -type Req struct { - ID int `param:"id" validate:"gte=10"` +type Body struct { + ID int `json:"..."` + Q int `json:"..."` + Likes []int `json:"..."` + T time.Time `json:"..."` + Token string `json:"..."` } -type Validator struct{} - -func (validator *Validator) Validate(v any) error { - return nil -} func main() { - app := fiber.New(fiber.Config{ - Validator: &Validator{}, - }) + app := fiber.New() app.Get("/:id", func(c fiber.Ctx) error { - var req Req - if err := c.BindWithValidate(&req); err != nil { + var data Body + if err := c.Bind().JSON(&data).Err(); err != nil { return err } - - return nil + return c.JSON(data) }) } ``` -### Parse Request Body +### Bind With validation -you can call `ctx.BodyJSON(v any) error` or `BodyXML(v any) error` +Normally, `bind` will only try to unmarshal data from request and pass it to request handler. -These methods will check content-type HTTP header and call configured JSON or XML decoder to unmarshal. +you can call `.Validate()` to validate previous binding. -```golang +And you will need to set a validator in app Config, otherwise it will always return an error. + +```go package main -type Body struct { - ID int `json:"..."` - Q int `json:"..."` - Likes []int `json:"..."` - T time.Time `json:"..."` - Token string `json:"..."` +type Validator struct{} + +func (validator *Validator) Validate(v any) error { + return nil } func main() { - app := fiber.New() + app := fiber.New(fiber.Config{ + Validator: &Validator{}, + }) app.Get("/:id", func(c fiber.Ctx) error { - var data Body - if err := c.BodyJSON(&data); err != nil { + var req struct{} + var body struct{} + if err := c.Bind().Req(&req).Validate().JSON(&body).Validate().Err(); err != nil { return err } - return c.JSON(data) + + return nil }) } ```