Gin 使用示例(十一):自定义表单验证器
我们还可以注册自定义的表单验证器,示例代码如下(src/gin-demo/examples/validator.go
):
package main
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"gopkg.in/go-playground/validator.v9"
)
// 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" 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.After(date) {
return false
}
}
return true
}
func getBookable(c *gin.Context) {
var b Booking
if err := c.ShouldBindWith(&b, binding.Query); err == nil {
c.JSON(http.StatusOK, gin.H{"message": "预定日期有效!"})
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
}
func main() {
route := gin.Default()
// 注册新的自定义验证规则
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterValidation("bookabledate", bookableDate)
}
route.GET("/bookable", getBookable)
route.Run(":8085")
}
启动服务器,测试结果如下:
2 Comments
Undefined validation function 'bookabledate' on field 'CheckIn'
将"gopkg.in/go-playground/validator.v9"改为"github.com/go-playground/validator/v10"即可