34 lines
723 B
Go
34 lines
723 B
Go
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
"service/api/base"
|
|
"service/library/validator"
|
|
)
|
|
|
|
func JSONParamValidator(p interface{}) gin.HandlerFunc {
|
|
return func(ctx *gin.Context) {
|
|
v := reflect.New(reflect.TypeOf(p))
|
|
err := ctx.ShouldBindBodyWith(v.Interface(), binding.JSON)
|
|
if !base.CheckBadRequest(ctx, err) {
|
|
return
|
|
}
|
|
|
|
//非空校验
|
|
if notNullItem, ok := v.Interface().(validator.NotNullItem); ok {
|
|
err := validator.GetDefaultNotNullValidator().Validate(notNullItem)
|
|
if err != nil {
|
|
base.CheckBadRequest(ctx, fmt.Errorf("非空校验失败: %v", err))
|
|
}
|
|
}
|
|
|
|
ctx.Set("client_req", v.Interface())
|
|
ctx.Next()
|
|
}
|
|
}
|