Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Aug 23, 2022
1 parent 4555649 commit b9c4e33
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions bind_readme.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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
})
}
```

0 comments on commit b9c4e33

Please sign in to comment.