-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidation.go
217 lines (200 loc) · 4.63 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
package gorql
import (
"errors"
"fmt"
"math"
"reflect"
"strconv"
"time"
)
type ValidationFunc func(*RqlNode) error
func errorType(v interface{}, expected string) error {
actual := "nil"
if v != nil {
actual = reflect.TypeOf(v).Kind().String()
}
return fmt.Errorf("expect <%s>, got <%s>", expected, actual)
}
// validate that the underlined element of given interface is a boolean.
func validateBool(v interface{}) error {
if _, ok := v.(bool); !ok {
return errorType(v, "bool")
}
return nil
}
// validate that the underlined element of given interface is a string.
func validateString(v interface{}) error {
if _, ok := v.(string); !ok {
return errorType(v, "string")
}
return nil
}
// validate that the underlined element of given interface is a float.
func validateFloat(v interface{}) error {
if _, ok := v.(float64); !ok {
return errorType(v, "float64")
}
return nil
}
// validate that the underlined element of given interface is an int.
func validateInt(v interface{}) error {
n, ok := v.(float64)
if !ok {
return errorType(v, "int")
}
if math.Trunc(n) != n {
return errors.New("not an integer")
}
return nil
}
// validate that the underlined element of given interface is an int and greater than 0.
func validateUInt(v interface{}) error {
if err := validateInt(v); err != nil {
return err
}
if v.(float64) < 0 {
return errors.New("not an unsigned integer")
}
return nil
}
// validate that the underlined element of this interface is a "datetime" string.
func validateTime(layout string) func(interface{}) error {
return func(v interface{}) error {
s, ok := v.(string)
if !ok {
return errorType(v, "string")
}
_, err := time.Parse(layout, s)
return err
}
}
func (p *Parser) validateFields(n *RqlNode) error {
if n == nil {
return nil
}
fn := p.GetFieldValidationFunc()
if fn == nil {
return fmt.Errorf("no field validation op '%s'", n.Op)
}
return fn(n)
}
func (p *Parser) GetFieldValidationFunc() ValidationFunc {
return func(n *RqlNode) (err error) {
var field *field
for i, a := range n.Args {
switch v := a.(type) {
case string:
if i == 0 {
f, ok := p.fields[v]
if !ok || !f.Filterable {
return fmt.Errorf("field name (arg: %s) is not filterable", v)
}
field = f
if field.ReplaceWith != "" {
n.Args[i] = field.ReplaceWith
}
} else {
if field == nil {
return fmt.Errorf("no field is found for node value %s", v)
}
newVal, err := field.CovertFn(v)
if err != nil {
return fmt.Errorf("encounter field error: %s", err)
}
n.Args[i] = newVal
}
case *RqlNode:
err = p.validateFields(v)
if err != nil {
return err
}
}
}
return nil
}
}
func (p *Parser) validateSpecialOps(r *RqlRootNode) error {
if r.Limit() != "" {
err := p.validateLimit(r.Limit())
if err != nil {
return err
}
}
if r.Offset() != "" {
err := p.validateOffset(r.Offset())
if err != nil {
return err
}
}
if p.c != nil && len(r.Sort()) > 0 {
err := p.validateSort(r.Sort())
if err != nil {
return err
}
}
if p.c != nil && len(r.Selects()) > 0 {
fieldNames, err := p.validateSelects(r.Selects())
if err != nil {
return err
}
r.selects = fieldNames
}
return nil
}
func (p *Parser) validateSort(sortItems []Sort) error {
for i, s := range sortItems {
f, ok := p.fields[s.By]
if !ok || !f.Sortable {
return fmt.Errorf("field %s is not sortable", s.By)
}
if f.ReplaceWith != "" {
sortItems[i].By = f.ReplaceWith
}
}
return nil
}
func (p *Parser) validateOffset(o string) error {
offset, err := strconv.Atoi(o)
if err != nil {
return fmt.Errorf("invalid format for offset: %s", err)
}
if offset < 0 {
return fmt.Errorf("offset is less than zero")
}
return nil
}
func (p *Parser) validateLimit(l string) error {
limit, err := strconv.Atoi(l)
if err != nil {
return fmt.Errorf("invalid format for limit: %s", err)
}
if limit < 0 {
return fmt.Errorf("specified limit is less than zero")
}
if p.c != nil && limit > p.c.LimitMaxValue {
return fmt.Errorf("specified limit is more than the max limit %d allowed", p.c.LimitMaxValue)
}
return nil
}
func (p *Parser) validateSelects(selects []string) (fieldNames []string, err error) {
for _, s := range selects {
f, ok := p.fields[s]
if !ok {
return nil, fmt.Errorf("field %s is projectable", s)
}
fieldName := f.Name
if f.ReplaceWith != "" {
fieldName = f.ReplaceWith
}
fieldNames = append(fieldNames, fieldName)
}
return
}
func IsValidField(s string) bool {
for _, ch := range s {
if !IsLetter(ch) && !IsDigit(ch) && ch != '_' && ch != '-' && ch != '.' {
return false
}
}
return true
}