Gin验证请求参数-自定义验证规则

Gin对请求参数自定义验证规则可以分三步:

  • 自定义结构体验证绑定binding标签
  • 针对该标签定义验证方法
  • 再将该验证方法注册到validator验证器里面

自定义结构体验证绑定binding标签

需要在请求参数结构体后面binding,加入自定义验证标签,如bookabledate标签,用于验证预约时间必须大于今天

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=CheckOut,bookabledate" time_format:"2006-01-02"`
}

 针对该标签定义验证方法

方法名自定义,同时需要引入"gopkg.in/go-playground/validator.v8"这个包,传入参数于下面例子里保持一致即可,通过断言从field字段拿到需要验证参数,再通过相应逻辑判断返回true和false,实现参数验证

//定义bookabledate标签对应的验证方法
func bookableDate(
	v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value,
	field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string,
) bool {
	if date, ok := field.Interface().(time.Time); ok {
		today := time.Now()

		if today.Year() > date.Year() || today.YearDay() > date.YearDay() {
			return false
		}
	}
	return true
}

 将该验证方法注册到validator验证器里面

将该验证方法注册到validator验证器里面,要注意标签与验证方法的对应,具体使用方法如下面代码:

 //将验证方法注册到验证器中
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		v.RegisterValidation("bookabledate", bookableDate)
	}

 完整例子如下

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"gopkg.in/go-playground/validator.v8"
	"net/http"
	"reflect"
	"time"
)

//binding 绑定一些验证请求参数,自定义标签bookabledate表示可预约的时期
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=CheckOut,bookabledate" time_format:"2006-01-02"`
}


//定义bookabledate标签对应的验证方法
func bookableDate(
	v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value,
	field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string,
) bool {
	if date, ok := field.Interface().(time.Time); ok {
		today := time.Now()

		if  date.Unix() > today.Unix() {
			return true
		}
	}
	return false
}


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()})
	}
}

测试结果:

devops$ curl -X GET http://localhost:8080/bookable?check_in=2019-09-20&check_out=2019-09-21
{"error":"Key: 'Booking.CheckOut' Error:Field validation for 'CheckOut' failed on the 'required' tag"}
devops$ curl -X GET http://localhost:8080/bookable?check_in=2019-11-20&check_out=2019-11-21

{"message": "Booking dates are valid!"}

如有问题,欢迎指正,相互学习,共同进步。

 

你可能感兴趣的:(Golang进阶之路,Golang,Gin,golang,参数验证)