From 9cbb254cc0f70ab418fcfa6f425df230911ee55e Mon Sep 17 00:00:00 2001 From: fastbpmn Date: Mon, 27 Feb 2023 14:17:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dvalidator=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=B7=AE=E5=BC=82=E5=AF=BC=E8=87=B4=E7=9A=84=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom-validation/server.go | 59 ++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/custom-validation/server.go b/custom-validation/server.go index ffc7028..260a11b 100644 --- a/custom-validation/server.go +++ b/custom-validation/server.go @@ -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() @@ -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 +}