Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Idan-Fishman/fiber-bind
🧬 Request schema validator middleware for Fiber
https://github.com/Idan-Fishman/fiber-bind
fiber go golang middleware performance validation web
Last synced: 3 months ago
JSON representation
🧬 Request schema validator middleware for Fiber
- Host: GitHub
- URL: https://github.com/Idan-Fishman/fiber-bind
- Owner: Idan-Fishman
- License: mit
- Created: 2023-04-28T06:56:55.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-14T21:37:04.000Z (about 1 year ago)
- Last Synced: 2024-06-21T18:04:14.678Z (5 months ago)
- Topics: fiber, go, golang, middleware, performance, validation, web
- Language: Go
- Homepage: https://pkg.go.dev/github.com/idan-fishman/fiber-bind
- Size: 54.7 KB
- Stars: 10
- Watchers: 1
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-fiber - Idan-Fishman/fiber-bind - Request schema validator middleware that validates sources such as the request body, query string parameters, route parameters and even form files. (⚙️ Middlewares / 🌱 Third Party)
README
# Bind Middleware for Fiber
[![Mentioned in Awesome Fiber](https://awesome.re/mentioned-badge.svg)](https://github.com/gofiber/awesome-fiber)
Bind is a request schema validator middleware for the [Fiber](https://github.com/gofiber/fiber) web framework. It provides a convenient way to parse and validate data from different sources such as the request body, query string parameters, and route parameters.
## Installation
Use go get to install the middleware:
```bash
go get -u github.com/idan-fishman/fiber-bind
```## Usage Example
Here are examples of how to use Bind to validate data from the request body and query parameters.
### Body Parameters
```go
package mainimport (
"github.com/gofiber/fiber/v2"
"github.com/idan-fishman/fiber-bind"
)type Person struct {
Name string `json:"name" validate:"required"`
Age int `json:"age" validate:"gte=18"`
}func main() {
app := fiber.New()app.Post("/person", bind.New(bind.Config{
Validator: validator.New(),
Source: bind.JSON,
}, &Person{}), func(c *fiber.Ctx) error {
person := c.Locals(bind.JSON).(*Person)
return c.JSON(person)
})app.Listen(":3000")
}
```### Query Parameters
```go
package mainimport (
"github.com/gofiber/fiber/v2"
"github.com/idan-fishman/fiber-bind"
)type PersonParams struct {
Name string `json:"name" validate:"required"`
}func main() {
app := fiber.New()app.Get("/person", bind.New(bind.Config{
Validator: validator.New(),
Source: bind.Query,
}, &PersonParams{}), func(c *fiber.Ctx) error {
params := c.Locals(bind.Query).(*PersonParams)
return c.JSON(params)
})app.Listen(":3000")
}
```In these examples, we define a struct that represents the data we want to validate. We then use Bind to validate the data from the request body or query parameters using the `New` function. We specify the source of the data as `bind.Body` or `bind.Query` and provide the struct as the schema to validate against. If the data is valid, it will be available in the context locals for further use.
## Configuration
Bind can be configured using the `Config` struct, which has the following fields:
- `Next` - A function that is called before the middleware is executed. If this function returns `true`, the middleware is skipped.
- `Validator` - A validator instance to use for validating the data. By default, the middleware uses the [go-playground/validator/v10](https://github.com/go-playground/validator/v10) package.
- `Source` - The source of the data to validate. This can be one of `bind.JSON`, `bind.XML`, `bind.Form`, `bind.Query`, or `bind.Params`.
- `FormFileFields` - A map where the key is the name of the file field in the form and the value is the form tag. If a field is specified here, the middleware will check if the file is uploaded in the request.## License
Bind is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.