Custom validators

It is also possible to register custom validators. See the example code.

  1. package main
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/gin-gonic/gin"
  6. "github.com/gin-gonic/gin/binding"
  7. "github.com/go-playground/validator/v10"
  8. )
  9. // Booking contains binded and validated data.
  10. type Booking struct {
  11. CheckIn time.Time `form:"check_in" binding:"required,bookabledate" time_format:"2006-01-02"`
  12. CheckOut time.Time `form:"check_out" binding:"required,gtfield=CheckIn,bookabledate" time_format:"2006-01-02"`
  13. }
  14. var bookableDate validator.Func = func(fl validator.FieldLevel) bool {
  15. date, ok := fl.Field().Interface().(time.Time)
  16. if ok {
  17. today := time.Now()
  18. if today.After(date) {
  19. return false
  20. }
  21. }
  22. return true
  23. }
  24. func main() {
  25. route := gin.Default()
  26. if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
  27. v.RegisterValidation("bookabledate", bookableDate)
  28. }
  29. route.GET("/bookable", getBookable)
  30. route.Run(":8085")
  31. }
  32. func getBookable(c *gin.Context) {
  33. var b Booking
  34. if err := c.ShouldBindWith(&b, binding.Query); err == nil {
  35. c.JSON(http.StatusOK, gin.H{"message": "Booking dates are valid!"})
  36. } else {
  37. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  38. }
  39. }
  1. $ curl "localhost:8085/bookable?check_in=2118-04-16&check_out=2118-04-17"
  2. {"message":"Booking dates are valid!"}
  3. $ curl "localhost:8085/bookable?check_in=2118-03-10&check_out=2118-03-09"
  4. {"error":"Key: 'Booking.CheckOut' Error:Field validation for 'CheckOut' failed on the 'gtfield' tag"}

Struct level validations can also be registered this way. See the struct-lvl-validation example to learn more.

Last modified August 1, 2020 : chore: update Custom validators (a39a8b4)