@@ -58,15 +58,33 @@ func (r Required) DefaultMessage() string {
58
58
}
59
59
60
60
type Min struct {
61
- Min int
61
+ Min float64
62
62
}
63
63
64
64
func ValidMin (min int ) Min {
65
+ return ValidMinFloat (float64 (min ))
66
+ }
67
+
68
+ func ValidMinFloat (min float64 ) Min {
65
69
return Min {min }
66
70
}
67
71
68
72
func (m Min ) IsSatisfied (obj interface {}) bool {
69
- num , ok := obj .(int )
73
+ var (
74
+ num float64
75
+ ok bool
76
+ )
77
+ switch reflect .TypeOf (obj ).Kind () {
78
+ case reflect .Float64 :
79
+ num , ok = obj .(float64 )
80
+ case reflect .Float32 :
81
+ ok = true
82
+ num = float64 (obj .(float32 ))
83
+ case reflect .Int :
84
+ ok = true
85
+ num = float64 (obj .(int ))
86
+ }
87
+
70
88
if ok {
71
89
return num >= m .Min
72
90
}
@@ -78,15 +96,33 @@ func (m Min) DefaultMessage() string {
78
96
}
79
97
80
98
type Max struct {
81
- Max int
99
+ Max float64
82
100
}
83
101
84
102
func ValidMax (max int ) Max {
103
+ return ValidMaxFloat (float64 (max ))
104
+ }
105
+
106
+ func ValidMaxFloat (max float64 ) Max {
85
107
return Max {max }
86
108
}
87
109
88
110
func (m Max ) IsSatisfied (obj interface {}) bool {
89
- num , ok := obj .(int )
111
+ var (
112
+ num float64
113
+ ok bool
114
+ )
115
+ switch reflect .TypeOf (obj ).Kind () {
116
+ case reflect .Float64 :
117
+ num , ok = obj .(float64 )
118
+ case reflect .Float32 :
119
+ ok = true
120
+ num = float64 (obj .(float32 ))
121
+ case reflect .Int :
122
+ ok = true
123
+ num = float64 (obj .(int ))
124
+ }
125
+
90
126
if ok {
91
127
return num <= m .Max
92
128
}
@@ -104,6 +140,10 @@ type Range struct {
104
140
}
105
141
106
142
func ValidRange (min , max int ) Range {
143
+ return ValidRangeFloat (float64 (min ), float64 (max ))
144
+ }
145
+
146
+ func ValidRangeFloat (min , max float64 ) Range {
107
147
return Range {Min {min }, Max {max }}
108
148
}
109
149
@@ -232,7 +272,7 @@ const (
232
272
233
273
// Requires a string(IP Address) to be within IP Pattern type inclusive.
234
274
type IPAddr struct {
235
- vaildtypes []int
275
+ Vaildtypes []int
236
276
}
237
277
238
278
// Requires an IP Address string to be exactly a given validation type (IPv4, IPv6, IPv4MappedIPv6, IPv4CIDR, IPv6CIDR, IPv4MappedIPv6CIDR OR IPAny)
@@ -241,11 +281,11 @@ func ValidIPAddr(cktypes ...int) IPAddr {
241
281
for _ , cktype := range cktypes {
242
282
243
283
if cktype != IPAny && cktype != IPv4 && cktype != IPv6 && cktype != IPv4MappedIPv6 && cktype != IPv4CIDR && cktype != IPv6CIDR && cktype != IPv4MappedIPv6CIDR {
244
- return IPAddr {vaildtypes : []int {None }}
284
+ return IPAddr {Vaildtypes : []int {None }}
245
285
}
246
286
}
247
287
248
- return IPAddr {vaildtypes : cktypes }
288
+ return IPAddr {Vaildtypes : cktypes }
249
289
}
250
290
251
291
func isWithCIDR (str string , l int ) bool {
@@ -306,7 +346,7 @@ func (i IPAddr) IsSatisfied(obj interface{}) bool {
306
346
l := len (str )
307
347
ret := getIPType (str , l )
308
348
309
- for _ , ck := range i .vaildtypes {
349
+ for _ , ck := range i .Vaildtypes {
310
350
311
351
if ret != None && (ck == ret || ck == IPAny ) {
312
352
@@ -430,7 +470,7 @@ const (
430
470
431
471
// Requires a string to be without invisible characters
432
472
type PureText struct {
433
- mode int
473
+ Mode int
434
474
}
435
475
436
476
func ValidPureText (m int ) PureText {
@@ -535,7 +575,7 @@ func (p PureText) IsSatisfied(obj interface{}) bool {
535
575
if str , ok := obj .(string ); ok {
536
576
537
577
var ret bool
538
- switch p .mode {
578
+ switch p .Mode {
539
579
case STRICT :
540
580
ret , _ = isPureTextStrict (str )
541
581
case NORMAL :
@@ -564,7 +604,7 @@ var checkDenyRelativePath = regexp.MustCompile(`(?m)(` + regexDenyFileNameCharLi
564
604
565
605
// Requires an string to be sanitary file path
566
606
type FilePath struct {
567
- mode int
607
+ Mode int
568
608
}
569
609
570
610
func ValidFilePath (m int ) FilePath {
@@ -580,7 +620,7 @@ func (f FilePath) IsSatisfied(obj interface{}) bool {
580
620
if str , ok := obj .(string ); ok {
581
621
582
622
var ret bool
583
- switch f .mode {
623
+ switch f .Mode {
584
624
585
625
case ALLOW_RELATIVE_PATH :
586
626
ret = checkAllowRelativePath .MatchString (str )
0 commit comments