mirror of https://github.com/gofiber/fiber.git
add examples on valudator guid
parent
ef40c04ede
commit
0e2eae618b
|
@ -8,8 +8,7 @@ sidebar_position: 5
|
|||
|
||||
Fiber provides the [Bind](../api/bind.md#validation) function to validate and bind [request data](../api/bind.md#binders) to a struct.
|
||||
|
||||
```go title="Example"
|
||||
|
||||
```go title="Basic Example"
|
||||
import "github.com/go-playground/validator/v10"
|
||||
|
||||
type structValidator struct {
|
||||
|
@ -35,10 +34,78 @@ 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
|
||||
if err := c.Bind().Body(user); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(user)
|
||||
})
|
||||
```
|
||||
|
||||
```go title="Advanced Validation Example"
|
||||
type User struct {
|
||||
Name string `json:"name" validate:"required,min=3,max=32"`
|
||||
Email string `json:"email" validate:"required,email"`
|
||||
Age int `json:"age" validate:"gte=0,lte=100"`
|
||||
Password string `json:"password" validate:"required,min=8"`
|
||||
Website string `json:"website" validate:"url"`
|
||||
}
|
||||
|
||||
// Custom validation error messages
|
||||
type UserWithCustomMessages struct {
|
||||
Name string `json:"name" validate:"required,min=3,max=32" message:"Name is required and must be between 3 and 32 characters"`
|
||||
Email string `json:"email" validate:"required,email" message:"Valid email is required"`
|
||||
Age int `json:"age" validate:"gte=0,lte=100" message:"Age must be between 0 and 100"`
|
||||
}
|
||||
|
||||
app.Post("/user", func(c fiber.Ctx) error {
|
||||
user := new(User)
|
||||
|
||||
if err := c.Bind().Body(user); err != nil {
|
||||
// Handle validation errors
|
||||
if validationErrors, ok := err.(validator.ValidationErrors); ok {
|
||||
for _, e := range validationErrors {
|
||||
// e.Field() - field name
|
||||
// e.Tag() - validation tag
|
||||
// e.Value() - invalid value
|
||||
// e.Param() - validation parameter
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"field": e.Field(),
|
||||
"error": e.Error(),
|
||||
})
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(user)
|
||||
})
|
||||
```
|
||||
|
||||
```go title="Custom Validator Example"
|
||||
// Custom validator for password strength
|
||||
type PasswordValidator struct {
|
||||
validate *validator.Validate
|
||||
}
|
||||
|
||||
func (v *PasswordValidator) Validate(out any) error {
|
||||
if err := v.validate.Struct(out); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Custom password validation logic
|
||||
if user, ok := out.(*User); ok {
|
||||
if len(user.Password) < 8 {
|
||||
return errors.New("password must be at least 8 characters")
|
||||
}
|
||||
// Add more password validation rules here
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Usage
|
||||
app := fiber.New(fiber.Config{
|
||||
StructValidator: &PasswordValidator{validate: validator.New()},
|
||||
})
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue