Type string `validate:"required,min=3,max=32"`
Salary int `validate:"required,number"`
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"`
type ErrorResponse struct {
var validate = validator.New()
func ValidateStruct(user User) []*ErrorResponse {
var errors []*ErrorResponse
err := validate.Struct(user)
for _, err := range err.(validator.ValidationErrors) {
var element ErrorResponse
element.FailedField = err.StructNamespace()
element.Value = err.Param()
errors = append(errors, &element)
func AddUser(c *fiber.Ctx) error {
if err := c.BodyParser(user); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
errors := ValidateStruct(*user)
return c.Status(fiber.StatusBadRequest).JSON(errors)
// 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
// [{"FailedField":"User.Email","Tag":"required","Value":""},{"FailedField":"User.Job.Salary","Tag":"required","Value":""},{"FailedField":"User.Job.Type","Tag":"required","Value":""}]β