-
Notifications
You must be signed in to change notification settings - Fork 13
/
validate.go
304 lines (229 loc) · 6.35 KB
/
validate.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
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
package vertex
import (
"github.com/EverythingMe/vertex/schema"
"net/http"
"reflect"
"regexp"
"github.com/dvirsky/go-pylog/logging"
)
// Param validator interface
type validator interface {
Validate(v reflect.Value, r *http.Request) error
GetDefault() (interface{}, bool)
GetKey() string
IsOptional() bool
GetParamName() string
}
// Base param validator
type fieldValidator struct {
schema.ParamInfo
}
func (v *fieldValidator) Validate(field reflect.Value, r *http.Request) error {
//validate required fields
if v.Required {
if _, found := r.Form[v.Name]; !found || !field.IsValid() {
return MissingParamError("missing required param '%s'", v.Name)
}
}
return nil
}
func (v *fieldValidator) GetKey() string {
return v.StructKey
}
func (v *fieldValidator) GetParamName() string {
return v.Name
}
func (v *fieldValidator) IsOptional() bool {
return !v.Required
}
func (v *fieldValidator) GetDefault() (interface{}, bool) {
if v.Required {
return 0, false
}
return v.Default, v.HasDefault
}
func newFieldValidator(pi schema.ParamInfo) *fieldValidator {
ret := &fieldValidator{
ParamInfo: pi,
}
return ret
}
///////////////////////////////////////////////////////
//
// Int validator
//
///////////////////////////////////////////////////////
type intValidator struct {
*fieldValidator
}
func (v *intValidator) Validate(field reflect.Value, r *http.Request) error {
err := v.fieldValidator.Validate(field, r)
if err != nil {
return err
}
i := field.Int()
if v.HasMin && i < int64(v.Min) {
return InvalidParamError("Value too small for %s", v.GetParamName())
}
if v.HasMax && i > int64(v.Max) {
return InvalidParamError("Value too large for %s", v.GetParamName())
}
return nil
}
func newIntValidator(pi schema.ParamInfo) *intValidator {
return &intValidator{
fieldValidator: newFieldValidator(pi),
}
}
///////////////////////////////////////////////////////
//
// String validator
//
///////////////////////////////////////////////////////
type stringValidator struct {
*fieldValidator
re *regexp.Regexp
}
func (v *stringValidator) Validate(field reflect.Value, r *http.Request) error {
err := v.fieldValidator.Validate(field, r)
if err != nil {
return err
}
s := field.String()
if v.MaxLength > 0 && len(s) > v.MaxLength {
return InvalidParamError("%s is too long", v.GetParamName())
}
if v.MinLength > 0 && len(s) < v.MinLength {
return InvalidParamError("%s is too short", v.GetParamName())
}
if v.re != nil && !v.re.MatchString(s) {
return InvalidParamError("%s does not match regex pattern", v.GetParamName())
}
return nil
}
func newStringValidator(pi schema.ParamInfo) *stringValidator {
ret := &stringValidator{
fieldValidator: newFieldValidator(pi),
}
if pi.Pattern != "" {
re, err := regexp.Compile(pi.Pattern)
if err != nil {
logging.Error("Could not create regexp validator - invalid regexp: %s - %s", pi.Pattern, err)
} else {
ret.re = re
}
}
return ret
}
//////////////////////////////////////////////////
//
// Float validator
//
//////////////////////////////////////////////////
type floatValidator struct {
*fieldValidator
}
func (v *floatValidator) Validate(field reflect.Value, r *http.Request) error {
err := v.fieldValidator.Validate(field, r)
if err != nil {
return err
}
f := field.Float()
if v.HasMin && f < v.Min {
return InvalidParamError("Value too small for %s", v.GetParamName())
}
if v.HasMax && f > v.Max {
return InvalidParamError("Value too large for %s", v.GetParamName())
}
return nil
}
func newFloatValidator(pi schema.ParamInfo) *floatValidator {
ret := &floatValidator{
fieldValidator: newFieldValidator(pi),
}
return ret
}
//////////////////////////////////////////////////
//
// Bool validator
//
//////////////////////////////////////////////////
type boolValidator struct {
*fieldValidator
}
func (v *boolValidator) Validate(field reflect.Value, r *http.Request) error {
return v.fieldValidator.Validate(field, r)
}
func newBoolValidator(pi schema.ParamInfo) *boolValidator {
return &boolValidator{
fieldValidator: newFieldValidator(pi),
}
}
type RequestValidator struct {
fieldValidators []validator
}
func (rv *RequestValidator) Validate(request interface{}, r *http.Request) error {
val := reflect.ValueOf(request)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
//go over all the validators
for _, v := range rv.fieldValidators {
// find the field in the struct. we assume it's there since we build the validators on start time
field := val.FieldByName(v.GetKey())
// if the arg is optional and not set, we set the default
if v.IsOptional() && (!field.IsValid() || r.FormValue(v.GetParamName()) == "") {
def, ok := v.GetDefault()
if ok {
logging.Info("Default value for %s: %v", v.GetKey(), def)
field.Set(reflect.ValueOf(def).Convert(field.Type()))
}
}
// now we validate!
e := v.Validate(field, r)
if e != nil {
logging.Error("Could not validate field %s: %s", v.GetParamName(), e)
return e
}
}
return nil
}
// Create new request validator for a request handler interface.
// This function walks the struct tags of the handler's fields and extracts validation metadata.
//
// You should give it the reflect type of your request handler struct
func NewRequestValidator(ri schema.RequestInfo) *RequestValidator {
//if the user passes a pointer we walk the actual struct
ret := &RequestValidator{
fieldValidators: make([]validator, 0),
}
//iterate over the fields and create a validator for each
for _, pi := range ri.Params {
var vali validator
switch pi.Kind {
// case reflect.Struct:
// //for structs - we add validators recursively
// validator := NewRequestValidator(field.Type)
// if validator != nil && len(validator.fieldValidators) > 0 {
// ret.fieldValidators = append(ret.fieldValidators, validator.fieldValidators...)
// }
// continue
case reflect.String:
vali = newStringValidator(pi)
case reflect.Int, reflect.Int32, reflect.Int64:
vali = newIntValidator(pi)
case reflect.Float32, reflect.Float64:
vali = newFloatValidator(pi)
case reflect.Bool:
vali = newBoolValidator(pi)
default:
logging.Error("I don't know how to validate %s", pi.Kind)
continue
}
if vali != nil {
logging.Debug("Adding validator %v to request validator %v", vali, ri)
ret.fieldValidators = append(ret.fieldValidators, vali)
}
}
return ret
}