-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
params.go
101 lines (86 loc) · 2.31 KB
/
params.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package common
import (
"fmt"
"reflect"
"strings"
"github.com/ArtalkJS/Artalk/internal/i18n"
"github.com/ArtalkJS/Artalk/internal/log"
"github.com/gofiber/fiber/v2"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type Map = map[string]interface{}
func ParamsDecode(c *fiber.Ctx, destParams interface{}) (isContinue bool, resp error) {
reqMethod := c.Method()
// validate required
if isContinue, resp := ValidateRequired(c, destParams); !isContinue {
return false, resp
}
// parse query
if err := c.QueryParser(destParams); err != nil {
return false, err
}
// parse path params
if err := c.ParamsParser(destParams); err != nil {
return false, err
}
// parse body
if reqMethod == "POST" || reqMethod == "PUT" {
if err := c.BodyParser(destParams); err != nil {
return false, err
}
}
return true, nil
}
func ValidateRequired(c *fiber.Ctx, destParams interface{}) (isContinue bool, resp error) {
reqMethod := c.Method()
reqPath := c.Path()
refVal := reflect.ValueOf(destParams)
for i := 0; i < refVal.Elem().Type().NumField(); i++ {
k := refVal.Elem().Type().Field(i)
validateTag := k.Tag.Get("validate")
requiredField := (validateTag == "required")
if !requiredField {
continue // only check required field
}
// get param key
paramKey := ""
tagNames := []string{"query", "json", "form"}
for _, tagName := range tagNames {
paramKey = k.Tag.Get(tagName)
if paramKey != "" {
break
}
}
if paramKey == "" {
log.Errorf("[Validator] %s field '%s' real param key not found", reqPath, k.Name)
continue
}
// get param value
paramVal := func() string {
switch {
case reqMethod == "GET" || reqMethod == "DELETE":
return c.Query(paramKey)
case reqMethod == "POST" || reqMethod == "PUT":
var destMap map[string]interface{}
err := c.BodyParser(&destMap)
if err != nil {
log.Error("[Validator] "+reqPath+" field '"+k.Name+"' ", err)
return ""
}
if v, ok := destMap[paramKey]; ok {
return fmt.Sprintf("%v", v)
}
return ""
}
return ""
}()
// check required param
if strings.TrimSpace(paramVal) == "" {
return false, RespError(c, 400, i18n.T("{{name}} is required", map[string]interface{}{
"name": i18n.T(cases.Title(language.English, cases.Compact).String(paramKey)),
}))
}
}
return true, nil
}