gin/examples/time_formate/time_formate.go
2025-06-05 14:05:52 +08:00

53 lines
1.2 KiB
Go

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