Skip to content

✨ feature: custom validator #1766

Closed
balcieren wants to merge 4 commits intogofiber:masterfrom
balcieren:custom-validator
Closed

✨ feature: custom validator #1766
balcieren wants to merge 4 commits intogofiber:masterfrom
balcieren:custom-validator

Conversation

@balcieren
Copy link
Contributor

@balcieren balcieren commented Feb 11, 2022

Usage:

package main

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

type User struct {
    Name  string `json:"name" validate:"required"`
    Email string `json:"email" validate:"required,email"`
}

type CustomValidator struct {
    validator *validator.Validate
}


func (cv *CustomValidator) Validate(i interface{}) error {
  return cv.validator.Struct(i)
}

func main() {
  app := fiber.New(fiber.Config{
     Validator: &CustomValidator{validator: validator.New()},
  })

  app.Post("/users", func(c *fiber.Ctx) error {
    u := new(User)

    if err := c.BodyParser(u); err != nil {
      return fiber.NewError(StatusBadRequest, err.Error())
    }

    if err := c.Validate(u); err != nil {
      return fiber.NewError(StatusBadRequest, err.Error())
    }

    return c.JSON(u)
  })

  app.Listen(":3000")
}

@balcieren
Copy link
Contributor Author

@ReneWerner87 what do you think about this pr ?

@ReneWerner87
Copy link
Member

not sure, from the point of view this is good, but i am also only one of many maintainers

so we will probably first need a bit and discuss this

can imagine that someone could say that it is unnecessary, because you can implement it yourself without much code

so PR is in a feedback round

@ReneWerner87
Copy link
Member

regardless, thank you for your work

no matter what the decision is
unfortunately we always have to evaluate and see if the features are understandable and useful for the masses

@balcieren
Copy link
Contributor Author

balcieren commented Feb 14, 2022

@ReneWerner87 I have seen custom validator in echo and gin. so I wanted to add. It makes easier to access validator from context. But you are right. it needs to be discussed.

@hi019
Copy link
Contributor

hi019 commented Feb 15, 2022

Imo this isn't needed because users should just create a validator as Rene mentioned. Since Fiber doesn't use the validator internally, this seems like a very limited DI system

@ReneWerner87
Copy link
Member

Feature was rejected for the time being, we thank you for the effort anyway

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants