DEV Community

Cover image for Schema Validation in Go using Validator and Fiber
Francisco Mendes
Francisco Mendes

Posted on • Edited on

Schema Validation in Go using Validator and Fiber

Overview

One of the important things in creating a REST API is the validation of data coming from the frontend. That is, whenever our APIs are subject to receiving data from the request body (for example) it is always good to ensure that we are receiving the necessary properties and that the data types are correct.

And in golang we have some libraries that help us with this purpose and in this article we are going to use the validator library, which besides being perhaps the most popular, is the one that contains a good number of resources for us to learn on the internet.

In today's example we'll create a simple API, but then we'll proceed with implementing a middleware to validate the data before proceeding to the next handler.

Let's code

First let's install the following dependencies:



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


Enter fullscreen mode Exit fullscreen mode

Then let's create a simple API:



package main

import "github.com/gofiber/fiber/v2"

func main() {
    app := fiber.New()

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("Thank god it works 🙏")
    })

    app.Listen(":3000")
}


Enter fullscreen mode Exit fullscreen mode

Now let's create a struct called Dog with the properties that we expect to come in the request body.



package main

import "github.com/gofiber/fiber/v2"

type Dog struct {
    Name      string `json:"name" validate:"required,min=3,max=12"`
    Age       int    `json:"age" validate:"required,numeric"`
    IsGoodBoy bool   `json:"isGoodBoy" validate:"required"`
}

// ...


Enter fullscreen mode Exit fullscreen mode

Next we'll define the struct to standardize our error messages.



package main

import "github.com/gofiber/fiber/v2"

type Dog struct {
    Name      string `json:"name" validate:"required,min=3,max=12"`
    Age       int    `json:"age" validate:"required,numeric"`
    IsGoodBoy bool   `json:"isGoodBoy" validate:"required"`
}

type IError struct {
    Field string
    Tag   string
    Value string
}

// ...


Enter fullscreen mode Exit fullscreen mode

Now we can create a new instance of the validator and we can proceed with the creation of the middleware, which I'll give the name of ValidateAddDog.



package main

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

// ...

var Validator = validator.New()

func ValidateAddDog(c *fiber.Ctx) error {
  // ...
}

// ...


Enter fullscreen mode Exit fullscreen mode

Now let's validate the request body and if there was any error, the request will not go to the next handler and the errors will be sent. If there have not been any errors, the request will proceed to the next handler, like this:



package main

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

// ...

var Validator = validator.New()

func ValidateAddDog(c *fiber.Ctx) error {
    var errors []*IError
    body := new(Dog)
    c.BodyParser(&body)

    err := Validator.Struct(body)
    if err != nil {
        for _, err := range err.(validator.ValidationErrors) {
            var el IError
            el.Field = err.Field()
            el.Tag = err.Tag()
            el.Value = err.Param()
            errors = append(errors, &el)
        }
        return c.Status(fiber.StatusBadRequest).JSON(errors)
    }
    return c.Next()
}

// ...


Enter fullscreen mode Exit fullscreen mode

Now let's create a new route with the POST verb that will send the data sent by the client in the response body after being validated by our middleware.



package main

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

// ...

func main() {
    // ...

    app.Post("/", ValidateAddDog, func(c *fiber.Ctx) error {
        body := new(Dog)
        c.BodyParser(&body)
        return c.Status(fiber.StatusOK).JSON(body)
    })

    app.Listen(":3000")
}


Enter fullscreen mode Exit fullscreen mode

The final code should look like the following:



package main

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

type Dog struct {
    Name      string `json:"name" validate:"required,min=3,max=12"`
    Age       int    `json:"age" validate:"required,numeric"`
    IsGoodBoy bool   `json:"isGoodBoy" validate:"required"`
}

type IError struct {
    Field string
    Tag   string
    Value string
}

var Validator = validator.New()

func ValidateAddDog(c *fiber.Ctx) error {
    var errors []*IError
    body := new(Dog)
    c.BodyParser(&body)

    err := Validator.Struct(body)
    if err != nil {
        for _, err := range err.(validator.ValidationErrors) {
            var el IError
            el.Field = err.Field()
            el.Tag = err.Tag()
            el.Value = err.Param()
            errors = append(errors, &el)
        }
        return c.Status(fiber.StatusBadRequest).JSON(errors)
    }
    return c.Next()
}

func main() {
    app := fiber.New()

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("Thank god it works 🙏")
    })

    app.Post("/", ValidateAddDog, func(c *fiber.Ctx) error {
        body := new(Dog)
        c.BodyParser(&body)
        return c.Status(fiber.StatusOK).JSON(body)
    })

    app.Listen(":3000")
}


Enter fullscreen mode Exit fullscreen mode

The end result should look like the following:

final result

Conclusion

As always, I hope you found it interesting. If you noticed any errors in this article, please mention them in the comments. 🧑🏻‍💻

Hope you have a great day! 🎄

Top comments (1)

Collapse
 
kabir_singhshekhawat_06 profile image
Kabir Singh Shekhawat

Wondering what are the limits of this validator library, can it match Pydantic?