mirror of https://github.com/gofiber/fiber.git
📚 Doc: Add more validation examples (#3369)
* add examples on valudator guid * ref: return prev validation commentrevert-3200-jiejaitt-feature/add-user-context-support^2
parent
ef40c04ede
commit
7606c618d3
|
@ -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.
|
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"
|
import "github.com/go-playground/validator/v10"
|
||||||
|
|
||||||
type structValidator struct {
|
type structValidator struct {
|
||||||
|
@ -42,3 +41,71 @@ app.Post("/", func(c fiber.Ctx) error {
|
||||||
return c.JSON(user)
|
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