mirror of
https://github.com/gofiber/fiber.git
synced 2025-04-27 13:14:31 +00:00
* Add support for consistent documentation using markdownlint * Only run workflow during changes to markdown files * Fix more inconsistencies * Fixes to markdown under .github/ * More fixes * Apply suggestions from code review Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Fix typo in limiter docs * Add missing space before code-block * Add check for dead-links * Add write-good * Remove legacy README files * Fix glob for skipping .md files * Use paths-ignore instead --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1.1 KiB
1.1 KiB
id, title, sidebar_position
id | title | sidebar_position |
---|---|---|
validation | 🔎 Validation | 5 |
Validator package
Fiber provides the Bind function to validate and bind request data to a struct.
import "github.com/go-playground/validator/v10"
type structValidator struct {
validate *validator.Validate
}
// Validator needs to implement the Validate method
func (v *structValidator) Validate(out any) error {
return v.validate.Struct(out)
}
// Setup your validator in the config
app := fiber.New(fiber.Config{
StructValidator: &structValidator{validate: validator.New()},
})
type User struct {
Name string `json:"name" form:"name" query:"name" validate:"required"`
Age int `json:"age" form:"age" query:"age" validate:"gte=0,lte=100"`
}
app.Post("/", func(c fiber.Ctx) error {
user := new(User)
// Works with all bind methods - Body, Query, Form, ...
if err := c.Bind().Body(user); err != nil { // <- here you receive the validation errors
return err
}
return c.JSON(user)
})