使用V8版本
package main
import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"gopkg.in/go-playground/validator.v8"
"net/http"
"reflect"
"time"
)
//binding 绑定一些验证请求参数,自定义标签bookabledate表示可预约的时期
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=CheckOut,bookabledate" time_format:"2006-01-02"`
}
//定义bookabledate标签对应的验证方法
func bookableDate(
v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value,
field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string,
) bool {
if date, ok := field.Interface().(time.Time); ok {
today := time.Now()
if date.Unix() > today.Unix() {
return true
}
}
return false
}
func main() {
route := gin.Default()
//将验证方法注册到验证器中
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterValidation("bookabledate", bookableDate)
}
route.GET("/bookable", getBookable)
route.Run(":8080")
}
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!"})
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
}
使用V10版本
package main
import (
. "awesomeProject/start/src"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
)
func main() {
r := gin.Default()
if v, ok := binding.Validator.Engine().(*validator.Validate); ok{
v.RegisterValidation("topicurl", TopicUrl)
}
}
type Topic struct {
TopicUrl string `json:"url" binding:"omitempty,topicurl"`
}
func TopicUrl(fl validator.FieldLevel) bool {
if url,ok:=fl.Field().Interface().(string);ok{
if matched,_ := regexp.MatchString(`\w{4,10}`,url); matched{
return true
}
}
return false
}