Skip to content

Commit 250bdad

Browse files
Dean KarnDean Karn
authored andcommitted
Merge branch 'v2-development' into v2
Conflicts: validator.go
2 parents 191b1ce + e882184 commit 250bdad

File tree

3 files changed

+205
-112
lines changed

3 files changed

+205
-112
lines changed

baked_in.go

Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/url"
66
"reflect"
77
"strconv"
8+
"time"
89
)
910

1011
// BakedInValidators is the map of ValidationFunc used internally
@@ -43,9 +44,9 @@ func isURI(val interface{}, field interface{}, param string) bool {
4344
_, err := url.ParseRequestURI(field.(string))
4445

4546
return err == nil
46-
default:
47-
panic(fmt.Sprintf("Bad field type %T", field))
4847
}
48+
49+
panic(fmt.Sprintf("Bad field type %T", field))
4950
}
5051

5152
func isURL(val interface{}, field interface{}, param string) bool {
@@ -66,10 +67,9 @@ func isURL(val interface{}, field interface{}, param string) bool {
6667
}
6768

6869
return err == nil
69-
70-
default:
71-
panic(fmt.Sprintf("Bad field type %T", field))
7270
}
71+
72+
panic(fmt.Sprintf("Bad field type %T", field))
7373
}
7474

7575
func isEmail(val interface{}, field interface{}, param string) bool {
@@ -80,9 +80,9 @@ func isEmail(val interface{}, field interface{}, param string) bool {
8080

8181
case reflect.String:
8282
return emailRegex.MatchString(field.(string))
83-
default:
84-
panic(fmt.Sprintf("Bad field type %T", field))
8583
}
84+
85+
panic(fmt.Sprintf("Bad field type %T", field))
8686
}
8787

8888
func isHsla(val interface{}, field interface{}, param string) bool {
@@ -93,9 +93,9 @@ func isHsla(val interface{}, field interface{}, param string) bool {
9393

9494
case reflect.String:
9595
return hslaRegex.MatchString(field.(string))
96-
default:
97-
panic(fmt.Sprintf("Bad field type %T", field))
9896
}
97+
98+
panic(fmt.Sprintf("Bad field type %T", field))
9999
}
100100

101101
func isHsl(val interface{}, field interface{}, param string) bool {
@@ -106,9 +106,9 @@ func isHsl(val interface{}, field interface{}, param string) bool {
106106

107107
case reflect.String:
108108
return hslRegex.MatchString(field.(string))
109-
default:
110-
panic(fmt.Sprintf("Bad field type %T", field))
111109
}
110+
111+
panic(fmt.Sprintf("Bad field type %T", field))
112112
}
113113

114114
func isRgba(val interface{}, field interface{}, param string) bool {
@@ -119,9 +119,9 @@ func isRgba(val interface{}, field interface{}, param string) bool {
119119

120120
case reflect.String:
121121
return rgbaRegex.MatchString(field.(string))
122-
default:
123-
panic(fmt.Sprintf("Bad field type %T", field))
124122
}
123+
124+
panic(fmt.Sprintf("Bad field type %T", field))
125125
}
126126

127127
func isRgb(val interface{}, field interface{}, param string) bool {
@@ -132,9 +132,9 @@ func isRgb(val interface{}, field interface{}, param string) bool {
132132

133133
case reflect.String:
134134
return rgbRegex.MatchString(field.(string))
135-
default:
136-
panic(fmt.Sprintf("Bad field type %T", field))
137135
}
136+
137+
panic(fmt.Sprintf("Bad field type %T", field))
138138
}
139139

140140
func isHexcolor(val interface{}, field interface{}, param string) bool {
@@ -145,9 +145,9 @@ func isHexcolor(val interface{}, field interface{}, param string) bool {
145145

146146
case reflect.String:
147147
return hexcolorRegex.MatchString(field.(string))
148-
default:
149-
panic(fmt.Sprintf("Bad field type %T", field))
150148
}
149+
150+
panic(fmt.Sprintf("Bad field type %T", field))
151151
}
152152

153153
func isHexadecimal(val interface{}, field interface{}, param string) bool {
@@ -158,9 +158,9 @@ func isHexadecimal(val interface{}, field interface{}, param string) bool {
158158

159159
case reflect.String:
160160
return hexadecimalRegex.MatchString(field.(string))
161-
default:
162-
panic(fmt.Sprintf("Bad field type %T", field))
163161
}
162+
163+
panic(fmt.Sprintf("Bad field type %T", field))
164164
}
165165

166166
func isNumber(val interface{}, field interface{}, param string) bool {
@@ -171,9 +171,9 @@ func isNumber(val interface{}, field interface{}, param string) bool {
171171

172172
case reflect.String:
173173
return numberRegex.MatchString(field.(string))
174-
default:
175-
panic(fmt.Sprintf("Bad field type %T", field))
176174
}
175+
176+
panic(fmt.Sprintf("Bad field type %T", field))
177177
}
178178

179179
func isNumeric(val interface{}, field interface{}, param string) bool {
@@ -184,9 +184,9 @@ func isNumeric(val interface{}, field interface{}, param string) bool {
184184

185185
case reflect.String:
186186
return numericRegex.MatchString(field.(string))
187-
default:
188-
panic(fmt.Sprintf("Bad field type %T", field))
189187
}
188+
189+
panic(fmt.Sprintf("Bad field type %T", field))
190190
}
191191

192192
func isAlphanum(val interface{}, field interface{}, param string) bool {
@@ -197,9 +197,9 @@ func isAlphanum(val interface{}, field interface{}, param string) bool {
197197

198198
case reflect.String:
199199
return alphaNumericRegex.MatchString(field.(string))
200-
default:
201-
panic(fmt.Sprintf("Bad field type %T", field))
202200
}
201+
202+
panic(fmt.Sprintf("Bad field type %T", field))
203203
}
204204

205205
func isAlpha(val interface{}, field interface{}, param string) bool {
@@ -210,9 +210,9 @@ func isAlpha(val interface{}, field interface{}, param string) bool {
210210

211211
case reflect.String:
212212
return alphaRegex.MatchString(field.(string))
213-
default:
214-
panic(fmt.Sprintf("Bad field type %T", field))
215213
}
214+
215+
panic(fmt.Sprintf("Bad field type %T", field))
216216
}
217217

218218
func hasValue(val interface{}, field interface{}, param string) bool {
@@ -260,9 +260,18 @@ func isGte(val interface{}, field interface{}, param string) bool {
260260

261261
return st.Float() >= p
262262

263-
default:
264-
panic(fmt.Sprintf("Bad field type %T", field))
263+
case reflect.Struct:
264+
265+
if st.Type() == reflect.TypeOf(field) {
266+
267+
now := time.Now().UTC()
268+
t := field.(time.Time)
269+
270+
return t.After(now) || t.Equal(now)
271+
}
265272
}
273+
274+
panic(fmt.Sprintf("Bad field type %T", field))
266275
}
267276

268277
func isGt(val interface{}, field interface{}, param string) bool {
@@ -295,10 +304,15 @@ func isGt(val interface{}, field interface{}, param string) bool {
295304
p := asFloat(param)
296305

297306
return st.Float() > p
307+
case reflect.Struct:
298308

299-
default:
300-
panic(fmt.Sprintf("Bad field type %T", field))
309+
if st.Type() == reflect.TypeOf(field) {
310+
311+
return field.(time.Time).After(time.Now().UTC())
312+
}
301313
}
314+
315+
panic(fmt.Sprintf("Bad field type %T", field))
302316
}
303317

304318
// length tests whether a variable's length is equal to a given
@@ -334,10 +348,9 @@ func hasLengthOf(val interface{}, field interface{}, param string) bool {
334348
p := asFloat(param)
335349

336350
return st.Float() == p
337-
338-
default:
339-
panic(fmt.Sprintf("Bad field type %T", field))
340351
}
352+
353+
panic(fmt.Sprintf("Bad field type %T", field))
341354
}
342355

343356
// min tests whether a variable value is larger or equal to a given
@@ -380,9 +393,18 @@ func isLte(val interface{}, field interface{}, param string) bool {
380393

381394
return st.Float() <= p
382395

383-
default:
384-
panic(fmt.Sprintf("Bad field type %T", field))
396+
case reflect.Struct:
397+
398+
if st.Type() == reflect.TypeOf(field) {
399+
400+
now := time.Now().UTC()
401+
t := field.(time.Time)
402+
403+
return t.Before(now) || t.Equal(now)
404+
}
385405
}
406+
407+
panic(fmt.Sprintf("Bad field type %T", field))
386408
}
387409

388410
func isLt(val interface{}, field interface{}, param string) bool {
@@ -416,9 +438,15 @@ func isLt(val interface{}, field interface{}, param string) bool {
416438

417439
return st.Float() < p
418440

419-
default:
420-
panic(fmt.Sprintf("Bad field type %T", field))
441+
case reflect.Struct:
442+
443+
if st.Type() == reflect.TypeOf(field) {
444+
445+
return field.(time.Time).Before(time.Now().UTC())
446+
}
421447
}
448+
449+
panic(fmt.Sprintf("Bad field type %T", field))
422450
}
423451

424452
// max tests whether a variable value is lesser than a given

validator.go

Lines changed: 20 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"fmt"
1414
"reflect"
1515
"strings"
16+
"time"
1617
"unicode"
1718
)
1819

@@ -174,75 +175,6 @@ func (v *Validator) ValidateStruct(s interface{}) *StructValidationErrors {
174175
}
175176

176177
return v.validateStructRecursive(s, s)
177-
// structValue := reflect.ValueOf(s)
178-
// structType := reflect.TypeOf(s)
179-
// structName := structType.Name()
180-
181-
// validationErrors := &StructValidationErrors{
182-
// Struct: structName,
183-
// Errors: map[string]*FieldValidationError{},
184-
// StructErrors: map[string]*StructValidationErrors{},
185-
// }
186-
187-
// if structValue.Kind() == reflect.Ptr && !structValue.IsNil() {
188-
// return v.ValidateStruct(structValue.Elem().Interface())
189-
// }
190-
191-
// if structValue.Kind() != reflect.Struct && structValue.Kind() != reflect.Interface {
192-
// panic("interface passed for validation is not a struct")
193-
// }
194-
195-
// var numFields = structValue.NumField()
196-
197-
// for i := 0; i < numFields; i++ {
198-
199-
// valueField := structValue.Field(i)
200-
// typeField := structType.Field(i)
201-
202-
// if valueField.Kind() == reflect.Ptr && !valueField.IsNil() {
203-
// valueField = valueField.Elem()
204-
// }
205-
206-
// tag := typeField.Tag.Get(v.tagName)
207-
208-
// if tag == "-" {
209-
// continue
210-
// }
211-
212-
// // if no validation and not a struct (which may containt fields for validation)
213-
// if tag == "" && valueField.Kind() != reflect.Struct && valueField.Kind() != reflect.Interface {
214-
// continue
215-
// }
216-
217-
// switch valueField.Kind() {
218-
219-
// case reflect.Struct, reflect.Interface:
220-
221-
// if !unicode.IsUpper(rune(typeField.Name[0])) {
222-
// continue
223-
// }
224-
225-
// if structErrors := v.ValidateStruct(valueField.Interface()); structErrors != nil {
226-
// validationErrors.StructErrors[typeField.Name] = structErrors
227-
// // free up memory map no longer needed
228-
// structErrors = nil
229-
// }
230-
231-
// default:
232-
233-
// if fieldError := v.validateFieldByNameAndTag(valueField.Interface(), typeField.Name, tag); fieldError != nil {
234-
// validationErrors.Errors[fieldError.Field] = fieldError
235-
// // free up memory reference
236-
// fieldError = nil
237-
// }
238-
// }
239-
// }
240-
241-
// if len(validationErrors.Errors) == 0 && len(validationErrors.StructErrors) == 0 {
242-
// return nil
243-
// }
244-
245-
// return validationErrors
246178
}
247179

248180
// validateStructRecursive validates a struct recursivly and passes the top level struct around for use in validator functions and returns a struct containing the errors
@@ -296,10 +228,21 @@ func (v *Validator) validateStructRecursive(top interface{}, s interface{}) *Str
296228
continue
297229
}
298230

299-
if structErrors := v.validateStructRecursive(top, valueField.Interface()); structErrors != nil {
300-
validationErrors.StructErrors[typeField.Name] = structErrors
301-
// free up memory map no longer needed
302-
structErrors = nil
231+
if valueField.Type() == reflect.TypeOf(time.Time{}) {
232+
233+
if fieldError := v.validateFieldByNameAndTagAndValue(top, valueField.Interface(), typeField.Name, tag); fieldError != nil {
234+
validationErrors.Errors[fieldError.Field] = fieldError
235+
// free up memory reference
236+
fieldError = nil
237+
}
238+
239+
} else {
240+
241+
if structErrors := v.ValidateStruct(valueField.Interface()); structErrors != nil {
242+
validationErrors.StructErrors[typeField.Name] = structErrors
243+
// free up memory map no longer needed
244+
structErrors = nil
245+
}
303246
}
304247

305248
default:
@@ -363,7 +306,10 @@ func (v *Validator) validateFieldByNameAndTagAndValue(val interface{}, f interfa
363306
switch valueField.Kind() {
364307

365308
case reflect.Struct, reflect.Interface, reflect.Invalid:
366-
panic("Invalid field passed to ValidateFieldWithTag")
309+
310+
if valueField.Type() != reflect.TypeOf(time.Time{}) {
311+
panic("Invalid field passed to ValidateFieldWithTag")
312+
}
367313
}
368314

369315
var valErr *FieldValidationError

0 commit comments

Comments
 (0)