-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
validation.go
265 lines (229 loc) · 6.92 KB
/
validation.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package filter
import (
"fmt"
"strings"
"github.com/samber/lo"
"goyave.dev/goyave/v5"
"goyave.dev/goyave/v5/lang"
v "goyave.dev/goyave/v5/validation"
)
// Separator the separator used when parsing the query
var Separator = "||"
func init() {
lang.SetDefaultValidationRule("goyave-filter-filter.element", "The filter format is invalid.")
lang.SetDefaultValidationRule("goyave-filter-join.element", "The join format is invalid.")
lang.SetDefaultValidationRule("goyave-filter-sort.element", "The sort format is invalid.")
}
// FilterValidator checks the `filter` format and converts it to `*Filter` struct.
type FilterValidator struct {
v.BaseValidator
Or bool
}
// Validate checks the field under validation satisfies this validator's criteria.
func (v *FilterValidator) Validate(ctx *v.Context) bool {
if _, ok := ctx.Value.(*Filter); ok {
return true
}
str, ok := ctx.Value.(string)
if !ok {
return false
}
f, err := ParseFilter(str)
if err != nil {
return false
}
f.Or = v.Or
ctx.Value = f
return true
}
// Name returns the string name of the validator.
func (v *FilterValidator) Name() string { return "goyave-filter-filter" }
// IsType returns true
func (v *FilterValidator) IsType() bool { return true }
// SortValidator checks the `sort` format and converts it to `*Sort` struct.
type SortValidator struct {
v.BaseValidator
}
// Validate checks the field under validation satisfies this validator's criteria.
func (v *SortValidator) Validate(ctx *v.Context) bool {
if _, ok := ctx.Value.(*Sort); ok {
return true
}
str, ok := ctx.Value.(string)
if !ok {
return false
}
sort, err := ParseSort(str)
if err != nil {
return false
}
ctx.Value = sort
return true
}
// Name returns the string name of the validator.
func (v *SortValidator) Name() string { return "goyave-filter-sort" }
// IsType returns true
func (v *SortValidator) IsType() bool { return true }
// JoinValidator checks the `sort` format and converts it to `*Join` struct.
type JoinValidator struct {
v.BaseValidator
}
// FieldsValidator splits the string field under validation by comma and trims every element.
type FieldsValidator struct {
v.BaseValidator
}
// Validate checks the field under validation satisfies this validator's criteria.
func (v *FieldsValidator) Validate(ctx *v.Context) bool {
if ctx.Invalid {
return true
}
trim := func(s string, _ int) string { return strings.TrimSpace(s) }
if slice, ok := ctx.Value.([]string); ok {
ctx.Value = lo.Map(slice, trim)
return true
}
str := ctx.Value.(string)
ctx.Value = lo.Map(strings.Split(str, ","), trim)
return true
}
// Name returns the string name of the validator.
func (v *FieldsValidator) Name() string { return "goyave-filter-fields" }
// IsType returns true
func (v *FieldsValidator) IsType() bool { return true }
// Validate checks the field under validation satisfies this validator's criteria.
func (v *JoinValidator) Validate(ctx *v.Context) bool {
if _, ok := ctx.Value.(*Join); ok {
return true
}
str, ok := ctx.Value.(string)
if !ok {
return false
}
join, err := ParseJoin(str)
if err != nil {
return false
}
ctx.Value = join
return true
}
// Name returns the string name of the validator.
func (v *JoinValidator) Name() string { return "goyave-filter-join" }
// IsType returns true
func (v *JoinValidator) IsType() bool { return true }
// Validation returns a new RuleSet for query validation.
func Validation(_ *goyave.Request) v.RuleSet {
return v.RuleSet{
{Path: "filter", Rules: v.List{v.Array()}},
{Path: "filter[]", Rules: v.List{&FilterValidator{}}},
{Path: "or", Rules: v.List{v.Array()}},
{Path: "or[]", Rules: v.List{&FilterValidator{Or: true}}},
{Path: "sort", Rules: v.List{v.Array()}},
{Path: "sort[]", Rules: v.List{&SortValidator{}}},
{Path: "join", Rules: v.List{v.Array()}},
{Path: "join[]", Rules: v.List{&JoinValidator{}}},
{Path: "page", Rules: v.List{v.Int(), v.Min(1)}},
{Path: "per_page", Rules: v.List{v.Int(), v.Between(1, 500)}},
{Path: "search", Rules: v.List{v.String(), v.Max(255)}},
{Path: "fields", Rules: v.List{v.String(), &FieldsValidator{}}},
}
}
// ParseFilter parse a string in format "field||$operator||value" and return
// a Filter struct. The filter string must satisfy the used operator's "RequiredArguments"
// constraint, otherwise an error is returned.
func ParseFilter(filter string) (*Filter, error) {
res := &Filter{}
f := filter
op := ""
index := strings.Index(f, Separator)
if index == -1 {
return nil, fmt.Errorf("missing operator")
}
res.Field = strings.TrimSpace(f[:index])
if res.Field == "" {
return nil, fmt.Errorf("invalid filter syntax")
}
f = f[index+2:]
index = strings.Index(f, Separator)
if index == -1 {
index = len(f)
}
op = strings.TrimSpace(f[:index])
operator, ok := Operators[op]
if !ok {
return nil, fmt.Errorf("unknown operator: %q", f[:index])
}
res.Operator = operator
if index < len(f) {
f = f[index+2:]
for paramIndex := strings.Index(f, ","); paramIndex < len(f); paramIndex = strings.Index(f, ",") {
if paramIndex == -1 {
paramIndex = len(f)
}
p := strings.TrimSpace(f[:paramIndex])
if p == "" {
return nil, fmt.Errorf("invalid filter syntax")
}
res.Args = append(res.Args, p)
if paramIndex+1 >= len(f) {
break
}
f = f[paramIndex+1:]
}
}
if len(res.Args) < int(res.Operator.RequiredArguments) {
return nil, fmt.Errorf("operator %q requires at least %d argument(s)", op, res.Operator.RequiredArguments)
}
return res, nil
}
// ParseSort parse a string in format "name,ASC" and return a Sort struct.
// The element after the comma (sort order) must have a value allowing it to be
// converted to SortOrder, otherwise an error is returned.
func ParseSort(sort string) (*Sort, error) {
commaIndex := strings.Index(sort, ",")
if commaIndex == -1 {
return nil, fmt.Errorf("invalid sort syntax")
}
fieldName := strings.TrimSpace(sort[:commaIndex])
order := strings.TrimSpace(strings.ToUpper(sort[commaIndex+1:]))
if fieldName == "" || order == "" {
return nil, fmt.Errorf("invalid sort syntax")
}
if order != string(SortAscending) && order != string(SortDescending) {
return nil, fmt.Errorf("invalid sort order %q", order)
}
s := &Sort{
Field: fieldName,
Order: SortOrder(order),
}
return s, nil
}
// ParseJoin parse a string in format "relation||field1,field2,..." and return
// a Join struct.
func ParseJoin(join string) (*Join, error) {
separatorIndex := strings.Index(join, Separator)
if separatorIndex == -1 {
separatorIndex = len(join)
}
relation := strings.TrimSpace(join[:separatorIndex])
if relation == "" {
return nil, fmt.Errorf("invalid join syntax")
}
var fields []string
if separatorIndex+2 < len(join) {
fields = strings.Split(join[separatorIndex+2:], ",")
for i, f := range fields {
f = strings.TrimSpace(f)
if f == "" {
return nil, fmt.Errorf("invalid join syntax")
}
fields[i] = f
}
} else {
fields = nil
}
j := &Join{
Relation: relation,
Fields: fields,
}
return j, nil
}