Skip to content

Commit e882184

Browse files
Dean KarnDean Karn
authored andcommitted
issue-#11
update gt, gte, lt and let to handle dates compared to time.Now().UTC()
1 parent 5a0fdab commit e882184

File tree

3 files changed

+205
-43
lines changed

3 files changed

+205
-43
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(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(field interface{}, param string) bool {
@@ -66,10 +67,9 @@ func isURL(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(field interface{}, param string) bool {
@@ -80,9 +80,9 @@ func isEmail(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(field interface{}, param string) bool {
@@ -93,9 +93,9 @@ func isHsla(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(field interface{}, param string) bool {
@@ -106,9 +106,9 @@ func isHsl(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(field interface{}, param string) bool {
@@ -119,9 +119,9 @@ func isRgba(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(field interface{}, param string) bool {
@@ -132,9 +132,9 @@ func isRgb(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(field interface{}, param string) bool {
@@ -145,9 +145,9 @@ func isHexcolor(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(field interface{}, param string) bool {
@@ -158,9 +158,9 @@ func isHexadecimal(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(field interface{}, param string) bool {
@@ -171,9 +171,9 @@ func isNumber(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(field interface{}, param string) bool {
@@ -184,9 +184,9 @@ func isNumeric(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(field interface{}, param string) bool {
@@ -197,9 +197,9 @@ func isAlphanum(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(field interface{}, param string) bool {
@@ -210,9 +210,9 @@ func isAlpha(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(field interface{}, param string) bool {
@@ -260,9 +260,18 @@ func isGte(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(field interface{}, param string) bool {
@@ -295,10 +304,15 @@ func isGt(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(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(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(field interface{}, param string) bool {
@@ -416,9 +438,15 @@ func isLt(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 & 5 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

@@ -215,10 +216,21 @@ func (v *Validator) ValidateStruct(s interface{}) *StructValidationErrors {
215216
continue
216217
}
217218

218-
if structErrors := v.ValidateStruct(valueField.Interface()); structErrors != nil {
219-
validationErrors.StructErrors[typeField.Name] = structErrors
220-
// free up memory map no longer needed
221-
structErrors = nil
219+
if valueField.Type() == reflect.TypeOf(time.Time{}) {
220+
221+
if fieldError := v.validateFieldByNameAndTag(valueField.Interface(), typeField.Name, tag); fieldError != nil {
222+
validationErrors.Errors[fieldError.Field] = fieldError
223+
// free up memory reference
224+
fieldError = nil
225+
}
226+
227+
} else {
228+
229+
if structErrors := v.ValidateStruct(valueField.Interface()); structErrors != nil {
230+
validationErrors.StructErrors[typeField.Name] = structErrors
231+
// free up memory map no longer needed
232+
structErrors = nil
233+
}
222234
}
223235

224236
default:
@@ -270,7 +282,10 @@ func (v *Validator) validateFieldByNameAndTag(f interface{}, name string, tag st
270282
switch valueField.Kind() {
271283

272284
case reflect.Struct, reflect.Interface, reflect.Invalid:
273-
panic("Invalid field passed to ValidateFieldWithTag")
285+
286+
if valueField.Type() != reflect.TypeOf(time.Time{}) {
287+
panic("Invalid field passed to ValidateFieldWithTag")
288+
}
274289
}
275290

276291
var valErr *FieldValidationError

0 commit comments

Comments
 (0)