gin框架29--自定义验证器

gin框架29--自定义验证器

  • 介绍
  • 案例
  • 说明

介绍

本文介绍了如何在gin框架中自定义验证器,并加以案例说明。

案例

源码:

package main

import (
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
)

// Booking 包含绑定和验证的数据。
type Booking struct {
	CheckIn  time.Time `form:"check_in" binding:"required,bookabledate" time_format:"2006-01-02"`
	CheckOut time.Time `form:"check_out" binding:"required,gtfield=CheckIn,bookabledate" time_format:"2006-01-02"`
}

var bookableDate validator.Func = func(fl validator.FieldLevel) bool {
	date, ok := fl.Field().Interface().(time.Time)
	if ok {
		today := time.Now()
		if today.Year() > date.Year() || today.YearDay() > date.YearDay() {
			return false
		}
	}
	return true
}

func main() {
	route := gin.Default()

	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		v.RegisterValidation("bookabledate", bookableDate)
	}

	route.GET("/bookable", getBookable)
	route.Run(":8080")
}

func getBookable(c *gin.Context) {
	var b Booking
	if err := c.ShouldBindWith(&b, binding.Query); err == nil {
		c.JSON(http.StatusOK, gin.H{"message": "Booking dates are valid!"})
	} else {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	}
}

注: 官方文档中通过 today.After(date) 作为判断依据,该方法不准确,会导致一直为false, 因此可以调整为 today.Year() > date.Year() || today.YearDay() > date.YearDay()

测试:

http://127.0.0.1:8080/bookable?check_in=2022-04-01&check_out=2022-04-02
{
    "message": "Booking dates are valid!"
}

http://127.0.0.1:8080/bookable?check_in=2022-04-01&check_out=2022-04-01
{
    "error": "Key: 'Booking.CheckOut' Error:Field validation for 'CheckOut' failed on the 'gtfield' tag"
}

说明

gin官方文档 自定义验证器
gin-gonic/examples-> Struct level validations
gin自定义验证器转化中文

你可能感兴趣的:(Golang,golang,web,gin框架,自定义验证器,validator)