gin_valid/gin/b/b.go

63 lines
1.4 KiB
Go

package b
import (
"gin-valid/gin/binding"
"gin-valid/go-playground/validator/v10"
"mime"
"net/http"
)
type ValidError struct {
ErrString string
}
func (e *ValidError) Error() string {
return e.ErrString
}
func ShouldBind(req *http.Request, obj interface{}) error {
content, err := contentType(req)
if err != nil {
return err
}
b := binding.Default(req.Method, content)
err = ShouldBindWith(req, obj, b)
errs, ok := err.(validator.ValidationErrors)
if !ok {
// 非validator.ValidationErrors类型错误直接返回
return err
}
return errs.Translate(binding.ValidTrans)
}
func ShouldBindWith(req *http.Request, obj interface{}, b binding.Binding) error {
return b.Bind(req, obj)
}
func ShouldBindJSON(req *http.Request, obj interface{}) error {
return ShouldBindWith(req, obj, binding.JSON)
}
func ShouldBindHeader(req *http.Request, obj interface{}) error {
return ShouldBindWith(req, obj, binding.Header)
}
func ShouldBindQuery(req *http.Request, obj interface{}) error {
return ShouldBindWith(req, obj, binding.Query)
}
func contentType(r *http.Request) (string, error) {
ct := r.Header.Get("Content-Type")
if ct == "" {
ct = "application/octet-stream"
}
ct, _, err := mime.ParseMediaType(ct)
return ct, err
}
func filterFlags(content string) string {
for i, char := range content {
if char == ' ' || char == ';' {
return content[:i]
}
}
return content
}