Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复validator版本差异导致的验证错误 #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions custom-validation/server.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package main

import (
"net/http"
"time"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"github.com/go-playground/validator/v10" // 使用这个版本,Booing的验证关键字是binding,不可以使用validate,会导致bug
//"gopkg.in/go-playground/validator.v9" //使用这个版本,Booking的验证关键字是validate,不要使用binding
"net/http"
"time"
)

// Booking contains binded and validated data.
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 {
// 对于Booking的验证,要求check_in和check_out必须大于当前日期
// 若当前时间为2023-02-27,则check_in=2023-02-28,check_out=2023-03-01
var bookabledate validator.Func = func(fl validator.FieldLevel) bool {
date, ok := fl.Field().Interface().(time.Time)
if ok {
today := time.Now()
Expand All @@ -27,21 +28,57 @@ var bookableDate validator.Func = func(fl validator.FieldLevel) bool {
}

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

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

route.GET("/bookable", getBookable)
route.Run(":8085")
//r.GET("/bookable", func(c *gin.Context) {
// var book Booking
// if err := c.ShouldBind(&book); err != nil {
// c.JSON(http.StatusInternalServerError, gin.H{
// "error": err.Error(),
// })
// c.Abort()
// return
// }
// if err := validate.Struct(book); err != nil {
// c.JSON(http.StatusInternalServerError, gin.H{
// "error": err.Error(),
// })
// c.Abort()
// return
// }
//
// c.JSON(http.StatusOK, gin.H{
// "message": "OK",
// "booking": book,
// })
//})

r.GET("/bookable", getBookable)
r.Run(":8085")
}

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!"})
c.JSON(http.StatusOK, gin.H{"message": "OK", "booking": b})
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
}

func bookabledate2(fl validator.FieldLevel) bool {
if date, ok := fl.Field().Interface().(time.Time); ok {
today := time.Now()
if date.Unix() > today.Unix() {
return true
}
}

return false
}