-
Notifications
You must be signed in to change notification settings - Fork 2
/
validators.go
325 lines (251 loc) · 6.62 KB
/
validators.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package validator
import (
"fmt"
"log"
"reflect"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/chop-dbhi/data-models-service/client"
)
const (
DateLayout = "2006-01-02"
DatetimeLayout = "2006-01-02 15:04:05"
)
// isZeroValue returns true if the value is equal to the type's respective
// zero value or is empty in the case of a slice, map, or array.
func isZeroValue(v interface{}) bool {
if v == nil {
return true
}
t := reflect.TypeOf(v)
switch t.Kind() {
case reflect.Array, reflect.Map, reflect.Slice:
return reflect.ValueOf(v).Len() == 0
default:
return v == reflect.Zero(t).Interface()
}
}
type Context map[string]interface{}
func (c Context) String() string {
var i int
toks := make([]string, len(c))
for k, v := range c {
if !isZeroValue(v) {
toks[i] = fmt.Sprintf("%s = %v", k, v)
i++
}
}
return fmt.Sprintf("{%s}", strings.Join(toks, ", "))
}
type ValidateFunc func(value string, cxt Context) *ValidationError
type Validator struct {
Name string
Description string
Validate ValidateFunc
RequiresValue bool
}
func (v *Validator) String() string {
return v.Name
}
var EncodingValidator = &Validator{
Name: "Encoding",
Description: "Validates a string only contains utf-8 characters.",
RequiresValue: true,
Validate: func(s string, cxt Context) *ValidationError {
if !utf8.ValidString(s) {
var bad []rune
for i, r := range s {
if r == utf8.RuneError {
bs, size := utf8.DecodeRuneInString(s[i:])
if size == 1 {
bad = append(bad, bs)
}
}
}
return &ValidationError{
Err: ErrBadEncoding,
Context: Context{
"badRunes": bad,
},
}
}
return nil
},
}
var EscapedQuotesValidator = &Validator{
Name: "EscapedQoutes",
Description: "Validates any quote characters in a string are escaped.",
RequiresValue: true,
Validate: func(s string, cxt Context) *ValidationError {
i := strings.Index(s, `"`)
for i != -1 {
if i == len(s)-1 || s[i+1] != '"' {
return &ValidationError{
Err: ErrBareQuote,
}
} else {
s = s[i+2:]
}
i = strings.Index(s, `"`)
}
return nil
},
}
// IntegerValidator validates the raw value is an integer.
var IntegerValidator = &Validator{
Name: "Integer",
Description: "Validates the input string is a valid integer.",
RequiresValue: true,
Validate: func(s string, cxt Context) *ValidationError {
if _, err := strconv.ParseInt(s, 10, 32); err != nil {
return &ValidationError{
Err: ErrTypeMismatchInt,
}
}
return nil
},
}
// BigIntegerValidator validates the raw value is an integer.
var BigIntegerValidator = &Validator{
Name: "BigInteger",
Description: "Validates the input string is a valid BigInteger.",
RequiresValue: true,
Validate: func(s string, cxt Context) *ValidationError {
if _, err := strconv.ParseInt(s, 10, 64); err != nil {
return &ValidationError{
Err: ErrTypeMismatchBigInt,
}
}
return nil
},
}
// NumberValidator validates the raw value is a number.
var NumberValidator = &Validator{
Name: "Number",
Description: "Validates the input string is a valid number (float).",
RequiresValue: true,
Validate: func(s string, cxt Context) *ValidationError {
if _, err := strconv.ParseFloat(s, 32); err != nil {
return &ValidationError{
Err: ErrTypeMismatchNum,
}
}
return nil
},
}
// DateValidator validates the raw value is date.
var DateValidator = &Validator{
Name: "Date",
Description: "Validates the input value is a valid date.",
RequiresValue: true,
Validate: func(s string, cxt Context) *ValidationError {
if _, err := time.Parse(DateLayout, s); err != nil {
// Since dates are a subset of datetimes, a datetime is also
// a valid date. The consumer will need to handle using only
// the date portion.
if err := DatetimeValidator.Validate(s, cxt); err == nil {
return nil
}
return &ValidationError{
Err: ErrTypeMismatchDate,
}
}
return nil
},
}
// DatetimeValidator validates the raw value is date.
var DatetimeValidator = &Validator{
Name: "Datetime",
Description: "Validates the input value is a valid date time.",
RequiresValue: true,
Validate: func(s string, cxt Context) *ValidationError {
if _, err := time.Parse(DatetimeLayout, s); err != nil {
return &ValidationError{
Err: ErrTypeMismatchDateTime,
}
}
return nil
},
}
// RequiredValidator validates the the raw value is not empty. This only applies
// to fields that are marked as required in the spec.
var RequiredValidator = &Validator{
Name: "Required",
Description: "Validates the input value is not empty.",
Validate: func(s string, cxt Context) *ValidationError {
if s == "" {
return &ValidationError{
Err: ErrRequiredValue,
}
}
return nil
},
}
// StringLengthValidator validates the string value does not exceed a
// pre-defined length.
var StringLengthValidator = &Validator{
Name: "String Length",
Description: "Validates the input value is not longer than a pre-defined length.",
RequiresValue: true,
Validate: func(s string, cxt Context) *ValidationError {
length := cxt["length"].(int)
if len(s) > length {
return &ValidationError{
Err: ErrLengthExceeded,
Context: Context{
"maxLength": length,
},
}
}
return nil
},
}
// BoundValidator binds a validator to a context.
type BoundValidator struct {
Validator *Validator
Context Context
}
func (b *BoundValidator) String() string {
return b.Validator.String()
}
func (b *BoundValidator) Validate(s string) *ValidationError {
return b.Validator.Validate(s, b.Context)
}
// Bind returns a bound validator given a validator and context.
func Bind(v *Validator, cxt Context) *BoundValidator {
return &BoundValidator{
Validator: v,
Context: cxt,
}
}
// BindFieldValidators returns a set of validators for the field.
func BindFieldValidators(f *client.Field) []*BoundValidator {
var vs []*BoundValidator
vs = append(vs, Bind(EncodingValidator, nil))
// vs = append(vs, Bind(EscapedQuotesValidator, nil))
if f.Required {
vs = append(vs, Bind(RequiredValidator, nil))
}
// Add type-specific validators.
switch f.Type {
case "string", "clob", "text":
if f.Length > 0 {
vs = append(vs, Bind(StringLengthValidator, Context{"length": f.Length}))
}
case "integer":
vs = append(vs, Bind(IntegerValidator, nil))
case "biginteger":
vs = append(vs, Bind(BigIntegerValidator, nil))
case "number", "float", "decimal":
vs = append(vs, Bind(NumberValidator, nil))
case "date":
vs = append(vs, Bind(DateValidator, nil))
case "datetime", "timestamp":
vs = append(vs, Bind(DatetimeValidator, nil))
default:
log.Printf("no validator for type '%s'", f.Type)
}
return vs
}