Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- fixed validation-guide #2517

Merged
merged 16 commits into from
Jun 27, 2023
193 changes: 139 additions & 54 deletions docs/guide/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,76 +8,161 @@ sidebar_position: 5

Fiber can make _great_ use of the validator package to ensure correct validation of data to store.

* [Official validator Github page \(Installation, use, examples..\).](https://github.com/go-playground/validator)
- [Official validator Github page \(Installation, use, examples..\).](https://github.com/go-playground/validator)

You can find the detailed descriptions of the _validations_ used in the fields contained on the structs below:

* [Detailed docs](https://pkg.go.dev/github.com/go-playground/validator?tab=doc)
- [Detailed docs](https://pkg.go.dev/github.com/go-playground/validator?tab=doc)

```go title="Validation Example"
type Job struct{
Type string `validate:"required,min=3,max=32"`
Salary int `validate:"required,number"`
}
package main

import (
"fmt"
"log"
"strings"

"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
)

type (
User struct {
Name string `validate:"required,min=5,max=20"` // Required field, min 5 char long max 20
Age int `validate:"required,teener"` // Required field, and client needs to implement our 'teener' tag format which we'll see later
}

ErrorResponse struct {
Error bool
FailedField string
Tag string
Value interface{}
}

XValidator struct {
validator *validator.Validate
}

GlobalErrorHandlerResp struct {
Success bool `json:"success"`
Message string `json:"message"`
}
)

// This is the validator instance
// for more information see: https://github.com/go-playground/validator
var validate = validator.New()

type User struct{
Name string `validate:"required,min=3,max=32"`
// use `*bool` here otherwise the validation will fail for `false` values
// Ref: https://github.com/go-playground/validator/issues/319#issuecomment-339222389
IsActive *bool `validate:"required"`
Email string `validate:"required,email,min=6,max=32"`
Job Job `validate:"dive"`
}
func (v XValidator) Validate(data interface{}) []ErrorResponse {
validationErrors := []ErrorResponse{}

errs := validate.Struct(data)
if errs != nil {
for _, err := range errs.(validator.ValidationErrors) {
// In this case data object is actually holding the User struct
var elem ErrorResponse

type ErrorResponse struct {
FailedField string
Tag string
Value string
elem.FailedField = err.Field() // Export struct field name
elem.Tag = err.Tag() // Export struct tag
elem.Value = err.Value() // Export field value
elem.Error = true

validationErrors = append(validationErrors, elem)
}
}

return validationErrors
}

var validate = validator.New()
func ValidateStruct(user User) []*ErrorResponse {
var errors []*ErrorResponse
err := validate.Struct(user)
if err != nil {
for _, err := range err.(validator.ValidationErrors) {
var element ErrorResponse
element.FailedField = err.StructNamespace()
element.Tag = err.Tag()
element.Value = err.Param()
errors = append(errors, &element)
}
}
return errors
func main() {
myValidator := &XValidator{
validator: validate,
}

app := fiber.New(fiber.Config{
// Global custom error handler
ErrorHandler: func(c *fiber.Ctx, err error) error {
return c.Status(fiber.StatusBadRequest).JSON(GlobalErrorHandlerResp{
Success: false,
Message: err.Error(),
})
},
})

// Custom struct validation tag format
myValidator.validator.RegisterValidation("teener", func(fl validator.FieldLevel) bool {
// User.Age needs to fit our needs, 12-18 years old.
return fl.Field().Int() >= 12 && fl.Field().Int() <= 18
})

app.Get("/", func(c *fiber.Ctx) error {
user := &User{
Name: c.Query("name"),
Age: c.QueryInt("age"),
}

// Validation
if errs := myValidator.Validate(user); len(errs) > 0 && errs[0].Error {
errMsgs := make([]string, 0)

for _, err := range errs {
errMsgs = append(errMsgs, fmt.Sprintf(
"[%s]: '%v' | Needs to implement '%s'",
err.FailedField,
err.Value,
err.Tag,
))
}

return &fiber.Error{
Code: fiber.ErrBadRequest.Code,
Message: strings.Join(errMsgs, " and "),
}
}

// Logic, validated with success
return c.SendString("Hello, World!")
})

log.Fatal(app.Listen(":3000"))
}

func AddUser(c *fiber.Ctx) error {
//Connect to database
/**
OUTPUT

user := new(User)
[1]
Request:

if err := c.BodyParser(user); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})

}
GET http://127.0.0.1:3000/

errors := ValidateStruct(*user)
if errors != nil {
return c.Status(fiber.StatusBadRequest).JSON(errors)

}
Response:

//Do something else here
{"success":false,"message":"[Name]: '' | Needs to implement 'required' and [Age]: '0' | Needs to implement 'required'"}

//Return user
return c.JSON(user)
}
[2]
Request:

GET http://127.0.0.1:3000/?name=efdal&age=9

Response:
{"success":false,"message":"[Age]: '9' | Needs to implement 'teener'"}

[3]
Request:

GET http://127.0.0.1:3000/?name=efdal&age=

Response:
{"success":false,"message":"[Age]: '0' | Needs to implement 'required'"}

[4]
Request:

GET http://127.0.0.1:3000/?name=efdal&age=18

Response:
Hello, World!

// Running a test with the following curl commands
// curl -X POST -H "Content-Type: application/json" --data "{\"name\":\"john\",\"isactive\":\"True\"}" http://localhost:8080/register/user
**/

// Results in
// [{"FailedField":"User.Email","Tag":"required","Value":""},{"FailedField":"User.Job.Salary","Tag":"required","Value":""},{"FailedField":"User.Job.Type","Tag":"required","Value":""}]⏎
```