-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalidating_visitor.go
More file actions
401 lines (309 loc) · 10.5 KB
/
validating_visitor.go
File metadata and controls
401 lines (309 loc) · 10.5 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package epsearchast
import (
"fmt"
"reflect"
"regexp"
"sort"
"strings"
"github.com/go-playground/validator/v10"
)
type validatingVisitor struct {
AllowedOperators map[string][]string
ColumnAliases map[string]string
ValueValidators map[string]string
FieldTypes map[string]FieldType
}
// use a single instance of Validate, it caches struct info
var validate *validator.Validate
func init() {
validate = validator.New()
}
var _ AstVisitor = (*validatingVisitor)(nil)
// Returns a new validatingVisitor though you should use the helper functions (e.g., [ValidateAstFieldAndOperators]) instead of this method
func NewValidatingVisitor(allowedOps map[string][]string, aliases map[string]string, valueValidators map[string]string, fieldTypeMap map[string]FieldType) (AstVisitor, error) {
for k, v := range aliases {
if len(k) > 0 && k[0] == '^' && k[len(k)-1] == '$' {
// We can't validate regular expression based aliases without being too rigid, and having a lot of validation complexity for an edge case.
// For example, you could declare an alias of `t.(a|b)` to `$1` (i.e., a or b) and then specify validators on just a or b.
continue
}
if _, ok := allowedOps[v]; !ok {
return nil, fmt.Errorf("alias from `%s` to `%s` points to a field not in the allowed ops", k, v)
}
}
for k, v := range valueValidators {
if len(k) > 0 && k[0] == '^' && k[len(k)-1] == '$' {
// We can't validate regular expression based aliases without being too rigid, and having a lot of validation complexity for an edge case.
// For example, you could declare an alias of `t.(a|b)` to `$1` (i.e., a or b) and then specify validators on just a or b.
continue
}
if _, ok := allowedOps[k]; !ok {
if target, ok := aliases[k]; ok {
// Supporting aliases for validators would be messy because it could be many to one.
return nil, fmt.Errorf("validator for field `%s` with type `%s` points to an alias of `%s` instead of the field", k, v, target)
} else {
return nil, fmt.Errorf("validator for field `%s` with type `%s` points to an unknown field", k, v)
}
}
}
ftMap := map[string]FieldType{}
for k, v := range fieldTypeMap {
ftMap[k] = v
if allowedOps[k] == nil {
return nil, fmt.Errorf("field type map for field `%s` is specified but this is an unknown field", k)
}
}
for k := range allowedOps {
if _, ok := ftMap[k]; !ok {
ftMap[k] = String
}
}
return &validatingVisitor{
AllowedOperators: allowedOps,
ColumnAliases: aliases,
ValueValidators: valueValidators,
FieldTypes: ftMap,
}, nil
}
func (v *validatingVisitor) PreVisit() error {
return nil
}
func (v *validatingVisitor) PostVisit() error {
return nil
}
func (v *validatingVisitor) PreVisitAnd(astNode *AstNode) (bool, error) {
return true, nil
}
func (v *validatingVisitor) PostVisitAnd(astNode *AstNode) error {
return nil
}
func (v *validatingVisitor) PreVisitOr(astNode *AstNode) (bool, error) {
return true, nil
}
func (v *validatingVisitor) PostVisitOr(astNode *AstNode) error {
return nil
}
func (v *validatingVisitor) VisitIn(astNode *AstNode) (bool, error) {
fieldName := astNode.Args[0]
if err := v.validateFieldAndValue("in", fieldName, astNode.Args[1:]...); err != nil {
return false, err
}
return false, nil
}
func (v *validatingVisitor) VisitEq(astNode *AstNode) (bool, error) {
fieldName := astNode.Args[0]
if err := v.validateFieldAndValue("eq", fieldName, astNode.Args[1]); err != nil {
return false, err
}
return false, nil
}
func (v *validatingVisitor) VisitLe(astNode *AstNode) (bool, error) {
fieldName := astNode.Args[0]
if err := v.validateFieldAndValue("le", fieldName, astNode.Args[1]); err != nil {
return false, err
}
return false, nil
}
func (v *validatingVisitor) VisitLt(astNode *AstNode) (bool, error) {
fieldName := astNode.Args[0]
if err := v.validateFieldAndValue("lt", fieldName, astNode.Args[1]); err != nil {
return false, err
}
return false, nil
}
func (v *validatingVisitor) VisitGe(astNode *AstNode) (bool, error) {
fieldName := astNode.Args[0]
if err := v.validateFieldAndValue("ge", fieldName, astNode.Args[1]); err != nil {
return false, err
}
return false, nil
}
func (v *validatingVisitor) VisitGt(astNode *AstNode) (bool, error) {
fieldName := astNode.Args[0]
if err := v.validateFieldAndValue("gt", fieldName, astNode.Args[1]); err != nil {
return false, err
}
return false, nil
}
func (v *validatingVisitor) VisitLike(astNode *AstNode) (bool, error) {
fieldName := astNode.Args[0]
if err := v.validateFieldAndValue("like", fieldName, astNode.Args[1]); err != nil {
return false, err
}
return false, nil
}
func (v *validatingVisitor) VisitILike(astNode *AstNode) (bool, error) {
fieldName := astNode.Args[0]
if err := v.validateFieldAndValue("ilike", fieldName, astNode.Args[1]); err != nil {
return false, err
}
return false, nil
}
func (v *validatingVisitor) VisitContains(astNode *AstNode) (bool, error) {
fieldName := astNode.Args[0]
if err := v.validateFieldAndValue("contains", fieldName, astNode.Args[1]); err != nil {
return false, err
}
return false, nil
}
func (v *validatingVisitor) VisitContainsAny(astNode *AstNode) (bool, error) {
fieldName := astNode.Args[0]
if err := v.validateFieldAndValue("contains_any", fieldName, astNode.Args[1:]...); err != nil {
return false, err
}
return false, nil
}
func (v *validatingVisitor) VisitContainsAll(astNode *AstNode) (bool, error) {
fieldName := astNode.Args[0]
if err := v.validateFieldAndValue("contains_all", fieldName, astNode.Args[1:]...); err != nil {
return false, err
}
return false, nil
}
func (v *validatingVisitor) VisitText(astNode *AstNode) (bool, error) {
fieldName := astNode.Args[0]
if err := v.validateFieldAndValue("text", fieldName, astNode.Args[1]); err != nil {
return false, err
}
return false, nil
}
func (v *validatingVisitor) VisitIsNull(astNode *AstNode) (bool, error) {
fieldName := astNode.Args[0]
if err := v.validateFieldAndValue("is_null", fieldName); err != nil {
return false, err
}
return false, nil
}
func (v *validatingVisitor) isOperatorValidForField(operator, requestField, canonicalField string) (bool, error) {
allowedOperatorsForField, ok, err := findMatchInMap(canonicalField, v.AllowedOperators)
if err != nil {
return false, err
}
if !ok {
allowedFields := reflect.ValueOf(v.AllowedOperators).MapKeys()
// Sort the allowed fields to give consistent errors
sortedAllowedFields := make([]string, len(allowedFields))
for i := range allowedFields {
sortedAllowedFields[i] = allowedFields[i].String()
}
sort.Strings(sortedAllowedFields)
return false, fmt.Errorf("unknown field [%s] specified in search filter, allowed fields are %v", requestField, sortedAllowedFields)
}
for _, op := range allowedOperatorsForField {
if strings.ToLower(operator) == strings.ToLower(op) {
return true, nil
}
}
return false, fmt.Errorf("unknown operator [%s] specified in search filter for field [%s], allowed operators are %v", strings.ToLower(operator), requestField, allowedOperatorsForField)
}
func (v *validatingVisitor) validateFieldAndValue(operator, requestField string, values ...string) error {
canonicalField, err := v.resolveFieldName(requestField)
if err != nil {
return err
}
if _, err := v.isOperatorValidForField(operator, requestField, canonicalField); err != nil {
return err
}
fieldType, ok, err := findMatchInMap(canonicalField, v.FieldTypes)
if err != nil {
return err
}
if !ok {
// This is almost certainly a bug, we should always get a string back if something wasn't set.
return fmt.Errorf("unknown field type for field [%s]", requestField)
}
for _, value := range values {
err := ValidateValue(fieldType, value)
if err != nil {
return fmt.Errorf("could not validate [%s], the value [%s] could not be converted to %s: %w", requestField, value, fieldType, err)
}
}
valueValidatorsForField, ok, err := findMatchInMap(canonicalField, v.ValueValidators)
if err != nil {
return err
}
if ok {
for _, value := range values {
vt, _ := Convert(fieldType, value)
err := validate.Var(vt, valueValidatorsForField)
if err != nil {
if verrors, ok := err.(validator.ValidationErrors); ok {
if len(verrors) > 0 {
verror := verrors[0]
return fmt.Errorf("could not validate [%s] with [%s], value [%v] does not satisfy requirement [%s]", requestField, operator, verror.Value(), verror.Tag())
}
}
return fmt.Errorf("could not validate [%s] with [%s] validation error: %w", requestField, value, err)
}
}
}
return nil
}
func (v *validatingVisitor) resolveFieldName(requestField string) (string, error) {
var matchedKeys []string
// Check for exact match
if _, ok := v.ColumnAliases[requestField]; ok {
matchedKeys = append(matchedKeys, requestField)
}
// Check for regex matches (even if we found an exact match, to detect ambiguity)
for k := range v.ColumnAliases {
if len(k) > 0 && k[0] == '^' && k[len(k)-1] == '$' {
r := regexp.MustCompile(k)
if r.MatchString(requestField) {
matchedKeys = append(matchedKeys, k)
}
}
}
if len(matchedKeys) > 1 {
sort.Strings(matchedKeys)
return "", fmt.Errorf("field [%s] matches multiple aliases in configuration: %v", requestField, matchedKeys)
}
// Apply the single matched alias (if any)
canonicalField := requestField
if len(matchedKeys) == 1 {
key := matchedKeys[0]
if len(key) > 0 && key[0] == '^' && key[len(key)-1] == '$' {
// Regex alias - apply replacement
r := regexp.MustCompile(key)
canonicalField = string(r.ReplaceAll([]byte(canonicalField), []byte(v.ColumnAliases[key])))
} else {
// Exact alias
canonicalField = v.ColumnAliases[key]
}
}
return canonicalField, nil
}
func findMatchInMap[T any](key string, m map[string]T) (T, bool, error) {
var matchedKeys []string
var matchedValue T
found := false
// Check for exact match
if v, ok := m[key]; ok {
matchedKeys = append(matchedKeys, key)
matchedValue = v
found = true
}
// Check for regex matches (even if we found an exact match, to detect ambiguity)
for k, v := range m {
if len(k) > 0 && k[0] == '^' && k[len(k)-1] == '$' {
r := regexp.MustCompile(k)
if r.MatchString(key) {
matchedKeys = append(matchedKeys, k)
if !found {
matchedValue = v
found = true
}
}
}
}
if len(matchedKeys) > 1 {
sort.Strings(matchedKeys)
var zero T
return zero, false, fmt.Errorf("field [%s] matches multiple keys in configuration: %v", key, matchedKeys)
}
if !found {
var zero T
return zero, false, nil
}
return matchedValue, true, nil
}