mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 04:08:15 +08:00
bindJSon的校验规则,待熟悉
This commit is contained in:
parent
bf5f26cf33
commit
305f2003c1
52
examples/time_formate/time_formate.go
Normal file
52
examples/time_formate/time_formate.go
Normal 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")
|
||||
}
|
@ -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",
|
||||
|
Loading…
x
Reference in New Issue
Block a user