From 305f2003c1d1adc585d6b36ce9a0c21aca34a74e Mon Sep 17 00:00:00 2001 From: flybread <1015126672@qq.com> Date: Thu, 5 Jun 2025 14:05:52 +0800 Subject: [PATCH] =?UTF-8?q?bindJSon=E7=9A=84=E6=A0=A1=E9=AA=8C=E8=A7=84?= =?UTF-8?q?=E5=88=99=EF=BC=8C=E5=BE=85=E7=86=9F=E6=82=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/time_formate/time_formate.go | 52 +++++++++++++++++++++++++++ examples/url/ini/utl_gin.go | 10 +++--- 2 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 examples/time_formate/time_formate.go diff --git a/examples/time_formate/time_formate.go b/examples/time_formate/time_formate.go new file mode 100644 index 00000000..f9074680 --- /dev/null +++ b/examples/time_formate/time_formate.go @@ -0,0 +1,52 @@ +package main + +import ( + "net/http" + "time" + + "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" + "github.com/go-playground/validator/v10" +) + +// Booking struct with validated time fields +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,bookabledate" time_format:"2006-01-02"` +} + +/** +请求的header类型需要是:application/x-www-form-urlencoded +*/ + +// bookableDate is a custom validator +var bookableDate = 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 main() { + g := gin.Default() + g.POST("/bookable", func(c *gin.Context) { + var b Booking + if err := c.ShouldBindWith(&b, binding.FormPost); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + // Validation successful + c.JSON(http.StatusOK, gin.H{"data": b}) + }) + + // Register the custom validator + if v, ok := binding.Validator.Engine().(*validator.Validate); ok { + v.RegisterValidation("bookabledate", bookableDate) + } + g.Run(":8080") +} diff --git a/examples/url/ini/utl_gin.go b/examples/url/ini/utl_gin.go index 649ab628..0c5c00ae 100644 --- a/examples/url/ini/utl_gin.go +++ b/examples/url/ini/utl_gin.go @@ -5,15 +5,17 @@ import ( "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "net/http" + "time" ) type Body struct { // json tag to de-serialize json body Name string `json:"name"` //For example, you can use struct tags to validate a custom product code format. The validator package offers many helpful string validator helpers. - ProductCode string `json:"productCode" binding:"required,startswith=PC,len=10"` - StartDate string `json:"start_date" binding:"required" time_format:"2006-01-02"` - EndDate string `json:"end_date" binding:"required" time_format:"2006-01-02"` + ProductCode string `json:"productCode" binding:"required,startswith=PC,len=10"` + StartDate string `json:"start_date" binding:"required" time_format:"2006-01-02"` + EndDate string `json:"end_date" binding:"required" time_format:"2006-01-02"` + EndDate1 time.Time `form:"end_date_1" binding:"required" time_format:"2006-01-02"` } func UrlInit(router *gin.Engine) { @@ -101,7 +103,7 @@ func UrlInit(router *gin.Engine) { // using BindJson method to serialize body with struct // BindJSON reads the body buffer to de-serialize it to a struct. // BindJSON cannot be called on the same context twice because it flushes the body buffer. - if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil { + if err := c.ShouldBind(&body); err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ "error": "VALIDATEERR-1",