bindJSon的校验规则,待熟悉

This commit is contained in:
flybread 2025-06-05 14:05:52 +08:00
parent bf5f26cf33
commit 305f2003c1
2 changed files with 58 additions and 4 deletions

View File

@ -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")
}

View File

@ -5,15 +5,17 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
"net/http" "net/http"
"time"
) )
type Body struct { type Body struct {
// json tag to de-serialize json body // json tag to de-serialize json body
Name string `json:"name"` 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. //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"` ProductCode string `json:"productCode" binding:"required,startswith=PC,len=10"`
StartDate string `json:"start_date" binding:"required" time_format:"2006-01-02"` StartDate string `json:"start_date" binding:"required" time_format:"2006-01-02"`
EndDate string `json:"end_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) { func UrlInit(router *gin.Engine) {
@ -101,7 +103,7 @@ func UrlInit(router *gin.Engine) {
// using BindJson method to serialize body with struct // using BindJson method to serialize body with struct
// BindJSON reads the body buffer to de-serialize it to a 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. // 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, c.AbortWithStatusJSON(http.StatusBadRequest,
gin.H{ gin.H{
"error": "VALIDATEERR-1", "error": "VALIDATEERR-1",