-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrongParamsRequired.go
114 lines (94 loc) · 3.17 KB
/
StrongParamsRequired.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
102
103
104
105
106
107
108
109
110
111
112
113
114
package strongparams
import (
"github.com/pkg/errors"
"github.com/thoas/go-funk"
"github.com/vellotis/go-strongparams/permitter"
"net/http"
"net/url"
"strings"
)
type StrongParamsRequired struct {
*strongParamsRequired
}
type strongParamsRequired struct {
*strongParams
requireKey *string
error error
}
// Permit instructs to apply the rules to whitelist the keys in url.Values before decoding it to the target struct.
// The chained Permit rules are applied on the object found behind the parameter `requireKey` defined key instructed by
// StrongParams.Require method.
// Go: Params().Require("root").Permit("sub:{key}")
// Whitelisted query: root[sub][key]=value
// These two use cases are equivalent:
// Permit("[key1, key2]")
// Permit("key1", "key2")
func (this *StrongParamsRequired) Permit(permitRule string, permitRules... string) *StrongParamsRequiredAndPermitted {
params := StrongParamsRequiredAndPermitted{
&strongParamsRequiredAndPermitted{
strongParamsRequired: this.strongParamsRequired,
},
}
if params.error == nil {
params.permitRules, params.error = permitter.ParsePermitted(
funk.Uniq(append(permitRules, permitRule)).([]string)...
)
}
return ¶ms
}
// Query instructs the mechanism to process http.Request's url.URL property url.URL/Query() method returned url.Values.
func (this *StrongParamsRequired) Query(request *http.Request) ReturnTarget {
return this.Values(request.URL.Query())
}
// PostForm instructs the mechanism to process http.Request's url.PostForm property's url.Values.
func (this *StrongParamsRequired) PostForm(request *http.Request) ReturnTarget {
return this.Values(request.PostForm)
}
// Values instructs the mechanism to process url.Values from `values` parameter.
func (this *StrongParamsRequired) Values(values url.Values) ReturnTarget {
values = cloneUrlValues(values)
if this.error == nil {
this.error = this.validate(values)
}
return func(target interface{}) error {
if this.error != nil {
return this.error
} else if target == nil {
return errors.New("`target` argument cannot be nil")
}
return this.validateTransformAndDecode(values, target)
}
}
func (this *strongParamsRequired) validateTransformAndDecode(values url.Values, target interface{}) (err error) {
if err := this.validate(values); err != nil {
return err
}
values, err = this.transform(values)
if err != nil {
return err
}
return this.decode(values, target)
}
func (this *strongParamsRequired) validate(values url.Values) error {
if this.requireKey != nil {
if !hasKey(values, *this.requireKey) {
return errors.Errorf("query: missing required key: `%s`", *this.requireKey)
}
}
return nil
}
func (this *strongParamsRequired) transform(values url.Values) (url.Values, error) {
if this.requireKey != nil {
requiredQueryValues := make(url.Values)
for path, value := range values {
if strings.HasPrefix(path, *this.requireKey+"[") {
newPath := strings.Replace(path, *this.requireKey+"[", "", 1)
newPath = strings.Replace(newPath, "]", "", 1)
requiredQueryValues[newPath] = make([]string, len(value))
copy(requiredQueryValues[newPath], value)
}
}
return requiredQueryValues, nil
}
return values, nil
}