-
Notifications
You must be signed in to change notification settings - Fork 3
/
validation.go
212 lines (172 loc) · 4.84 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
package validation
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"regexp"
"strings"
)
const DefaultKeyTag string = "form"
type Validation struct {
Errors Errors //reference to an object that implements the Errors interface
Obj interface{} //the top-most model struct being validated
Request *http.Request //optional feild that can allow us to do some http request validations
keyTag string //the key that errors will get mapped out to
}
//represents one 'set' of validation errors.
type Set struct {
Field interface{} //a pointer to the passed in feild
key string //string key pulled from the field
isValid bool
Error validationError
Validation *Validation //for now, keep a reference to the validation to map errors back
}
//call default if you want the internal Error struct implementation
func DefaultValidation(obj interface{}) *Validation {
errs := &ValidationErrors{}
return NewValidation(errs, obj)
}
//Call new validation to pass a custom Errors interface (martini Bind.Errors for example)
func NewValidation(errors Errors, obj interface{}) *Validation {
v := &Validation{Errors: errors, Obj: obj}
v.keyTag = DefaultKeyTag
return v
}
func (v *Validation) Validate(field interface{}) *Set {
s := &Set{Field: field, isValid: true, Validation: v}
key := v.getKeyForField(field)
s.key = key
return s
}
func (v *Validation) KeyTag(s string) {
v.keyTag = s
}
func (v *Validation) MapErrors() string {
if errSlice, ok := v.Errors.(*ValidationErrors); ok {
if b, err := json.Marshal(errSlice); err == nil {
return string(b)
}
}
return ""
}
//experimenting with trying to match the field with the passed in struct
func (v *Validation) getKeyForField(passedField interface{}) string {
typObj := reflect.TypeOf(v.Obj)
valObj := reflect.ValueOf(v.Obj)
typField := reflect.TypeOf(passedField)
valField := reflect.ValueOf(passedField)
//if our struct is a pointer, dereference it
if typObj.Kind() == reflect.Ptr {
typObj = typObj.Elem()
valObj = valObj.Elem()
}
//if our passed in field is a pointer, dereference it
if typField.Kind() == reflect.Ptr {
typField = typField.Elem()
valField = valField.Elem()
}
for i := 0; i < typObj.NumField(); i++ {
field := typObj.Field(i)
fieldValue := valObj.Field(i).Interface()
passedValue := valField.Interface()
if passedValue == fieldValue {
return field.Tag.Get(v.keyTag)
}
}
return ""
}
func (s *Set) Key(key string) *Set {
s.key = key
return s
}
func (s *Set) Required() *Set {
return s.validate(Required{}, s.Field)
}
// returns true if the validator has 1 or more errors
func (s *Set) HasErrors() bool {
return s.Validation.Errors.Len() > 0
}
func (s *Set) MaxLength(maxLength int) *Set {
return s.validate(MaxLength{MaxLength: maxLength}, s.Len())
}
func (s *Set) MinLength(minLength int) *Set {
return s.validate(MinLength{MinLength: minLength}, s.Len())
}
func (s *Set) Range(min int, max int) *Set {
return s.validate(Range{MinLength{MinLength: min}, MaxLength{MaxLength: max}}, s.Len())
}
func (s *Set) Match(strMatch string, regex *regexp.Regexp) *Set {
return s.validate(Matches{Regex: regex}, strMatch)
}
func (s *Set) NoMatch(strMatch string, regexp *regexp.Regexp) *Set {
return s.validate(NoMatch{Matches{Regex: regexp}}, strMatch)
}
func (s *Set) Email() *Set {
return s.validate(Email{Matches{emailPattern}}, s.toString())
}
func (s *Set) CreditCard() *Set {
return s.validate(CreditCard{Matches{creditCardPattern}}, s.toString())
}
func (s *Set) URL() *Set {
fmt.Println("URL: ", s.toString())
return s.validate(URL{Matches{urlPattern}}, s.toString())
}
//Utilities
func (s *Set) Classify(str string) *Set {
s.Error.class = str
return s
}
func (s *Set) Message(message string) *Set {
s.Error.msg = message
return s
}
func (s *Set) TrimSpace() *Set {
if str, ok := s.Field.(string); ok {
s.Field = strings.TrimSpace(str)
}
return s
}
func (s *Set) getMessage(val Validator) string {
if s.Error.msg == "" {
return val.DefaultMessage()
}
return s.Error.msg
}
func (s *Set) Len() int {
switch x := s.Field.(type) {
case string:
return len(x)
case *string:
return len(*x)
case []interface{}:
return len(x)
case *[]interface{}:
return len(*x)
case map[interface{}]interface{}:
return len(x)
case *map[interface{}]interface{}:
return len(*x)
}
return 0
}
func (s *Set) toString() string {
if str, ok := s.Field.(string); ok {
return str
}
if str, ok := s.Field.(*string); ok {
return *str
}
panic("This method requires a string value")
}
//runs the validation rule, returns true if the rule passed
func (s *Set) validate(validator Validator, obj interface{}) *Set {
//check if the rule is valid
if validator.IsValid(obj) {
return s
}
//else, add a new validation error
s.Validation.Errors.Add([]string{s.key}, s.Error.class, s.getMessage(validator))
s.isValid = false
return s
}