This package integrates Checker with the Fiber web framework. It binds a request body and runs Checker's struct tag validation in a single call, writing a JSON 400 response automatically when binding or validation fails.
go get github.com/cinar/checker/v2/fiberpackage main
import (
"github.com/gofiber/fiber/v3"
checkerfiber "github.com/cinar/checker/v2/fiber"
)
type Registration struct {
Name string `json:"name" checkers:"trim required"`
Email string `json:"email" checkers:"required email"`
}
func main() {
app := fiber.New()
app.Post("/register", func(c fiber.Ctx) error {
var registration Registration
if !checkerfiber.Bind(c, ®istration) {
// The 400 response has already been written by Bind.
return nil
}
return c.JSON(registration)
})
app.Listen(":8080")
}A POST /register with an invalid body, such as {"name":"","email":""}, gets back:
{
"name": {"code": "REQUIRED", "message": "Required value is missing."},
"email": {"code": "REQUIRED", "message": "Required value is missing."}
}If you have already bound the request body some other way, or you want to validate a struct assembled from more than one source (path params, query string, and body, for example), call Check directly instead:
app.Post("/register/:plan", func(c fiber.Ctx) error {
registration := Registration{
Plan: c.Params("plan"),
}
if err := c.Bind().Body(®istration); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
}
if !checkerfiber.Check(c, ®istration) {
return nil
}
return c.JSON(registration)
})This library is free to use, modify, and distribute under the terms of the MIT license found in the LICENSE file of the parent Checker repository.