-
Notifications
You must be signed in to change notification settings - Fork 174
/
api.go
68 lines (58 loc) · 1.53 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package api
import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
val "github.com/go-playground/validator/v10"
zhTranslations "github.com/go-playground/validator/v10/translations/zh"
"log"
"net/http"
"reflect"
)
func ErrHandler(c *gin.Context, err error) {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
}
type ValidError struct {
Key string
Message string
}
func BindAndValid(c *gin.Context, target interface{}) bool {
errs := make(map[string]string)
err := c.ShouldBindJSON(target)
if err != nil {
log.Println("raw err", err)
uni := ut.New(zh.New())
trans, _ := uni.GetTranslator("zh")
v, ok := binding.Validator.Engine().(*val.Validate)
if ok {
_ = zhTranslations.RegisterDefaultTranslations(v, trans)
}
verrs, ok := err.(val.ValidationErrors)
if !ok {
log.Println("verrs", verrs)
c.JSON(http.StatusNotAcceptable, gin.H{
"message": "Requested with wrong parameters",
"code": http.StatusNotAcceptable,
"error": verrs,
})
return false
}
for _, value := range verrs {
t := reflect.ValueOf(target)
realType := t.Type().Elem()
field, _ := realType.FieldByName(value.StructField())
errs[field.Tag.Get("json")] = value.Translate(trans)
}
c.JSON(http.StatusNotAcceptable, gin.H{
"errors": errs,
"message": "Requested with wrong parameters",
"code": http.StatusNotAcceptable,
})
return false
}
return true
}