-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.go
More file actions
64 lines (56 loc) · 1.78 KB
/
validator.go
File metadata and controls
64 lines (56 loc) · 1.78 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
package where
import (
"strings"
)
// Validator provides field and function allowlisting for security.
// It can be used to restrict which fields and functions are allowed in filter expressions.
type Validator struct {
allowedFields map[string]bool
allowedFunctions map[string]bool
allowAll bool
}
// NewValidator creates a new validator with empty allowlists.
// By default, all fields and functions are denied unless explicitly allowed.
func NewValidator() *Validator {
return &Validator{
allowedFields: make(map[string]bool),
allowedFunctions: make(map[string]bool),
allowAll: false,
}
}
// AllowAll configures the validator to allow all fields and functions.
// This disables security restrictions and should be used with caution.
func (v *Validator) AllowAll() *Validator {
v.allowAll = true
return v
}
// AllowFields adds the specified fields to the allowlist.
// Field names are case-insensitive.
func (v *Validator) AllowFields(fields ...string) *Validator {
for _, field := range fields {
v.allowedFields[strings.ToLower(field)] = true
}
return v
}
// AllowFunctions adds the specified functions to the allowlist.
// Function names are case-insensitive.
func (v *Validator) AllowFunctions(functions ...string) *Validator {
for _, fn := range functions {
v.allowedFunctions[strings.ToUpper(fn)] = true
}
return v
}
// IsFieldAllowed returns true if the field is allowed by this validator.
func (v *Validator) IsFieldAllowed(field string) bool {
if v.allowAll {
return true
}
return v.allowedFields[strings.ToLower(field)]
}
// IsFunctionAllowed returns true if the function is allowed by this validator.
func (v *Validator) IsFunctionAllowed(function string) bool {
if v.allowAll {
return true
}
return v.allowedFunctions[strings.ToUpper(function)]
}