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!"}

如有問題,歡迎指正,相互學習,共同進步。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章