Skip to content

Commit a6d8a1f

Browse files
authored
Merge pull request revel#1209 from notzippy/minmax_float
Add new float validators
2 parents 64861e9 + 051a561 commit a6d8a1f

File tree

3 files changed

+122
-44
lines changed

3 files changed

+122
-44
lines changed

validation.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,26 @@ func (v *Validation) Required(obj interface{}) *ValidationResult {
108108
}
109109

110110
func (v *Validation) Min(n int, min int) *ValidationResult {
111+
return v.MinFloat(float64(n), float64(min))
112+
}
113+
114+
func (v *Validation) MinFloat(n float64, min float64) *ValidationResult {
111115
return v.apply(Min{min}, n)
112116
}
113117

114118
func (v *Validation) Max(n int, max int) *ValidationResult {
119+
return v.MaxFloat(float64(n), float64(max))
120+
}
121+
122+
func (v *Validation) MaxFloat(n float64, max float64) *ValidationResult {
115123
return v.apply(Max{max}, n)
116124
}
117125

118126
func (v *Validation) Range(n, min, max int) *ValidationResult {
127+
return v.RangeFloat(float64(n), float64(min), float64(max))
128+
}
129+
130+
func (v *Validation) RangeFloat(n, min, max float64) *ValidationResult {
119131
return v.apply(Range{Min{min}, Max{max}}, n)
120132
}
121133

validators.go

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,33 @@ func (r Required) DefaultMessage() string {
5858
}
5959

6060
type Min struct {
61-
Min int
61+
Min float64
6262
}
6363

6464
func ValidMin(min int) Min {
65+
return ValidMinFloat(float64(min))
66+
}
67+
68+
func ValidMinFloat(min float64) Min {
6569
return Min{min}
6670
}
6771

6872
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+
7088
if ok {
7189
return num >= m.Min
7290
}
@@ -78,15 +96,33 @@ func (m Min) DefaultMessage() string {
7896
}
7997

8098
type Max struct {
81-
Max int
99+
Max float64
82100
}
83101

84102
func ValidMax(max int) Max {
103+
return ValidMaxFloat(float64(max))
104+
}
105+
106+
func ValidMaxFloat(max float64) Max {
85107
return Max{max}
86108
}
87109

88110
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+
90126
if ok {
91127
return num <= m.Max
92128
}
@@ -104,6 +140,10 @@ type Range struct {
104140
}
105141

106142
func ValidRange(min, max int) Range {
143+
return ValidRangeFloat(float64(min), float64(max))
144+
}
145+
146+
func ValidRangeFloat(min, max float64) Range {
107147
return Range{Min{min}, Max{max}}
108148
}
109149

@@ -232,7 +272,7 @@ const (
232272

233273
// Requires a string(IP Address) to be within IP Pattern type inclusive.
234274
type IPAddr struct {
235-
vaildtypes []int
275+
Vaildtypes []int
236276
}
237277

238278
// 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 {
241281
for _, cktype := range cktypes {
242282

243283
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}}
245285
}
246286
}
247287

248-
return IPAddr{vaildtypes: cktypes}
288+
return IPAddr{Vaildtypes: cktypes}
249289
}
250290

251291
func isWithCIDR(str string, l int) bool {
@@ -306,7 +346,7 @@ func (i IPAddr) IsSatisfied(obj interface{}) bool {
306346
l := len(str)
307347
ret := getIPType(str, l)
308348

309-
for _, ck := range i.vaildtypes {
349+
for _, ck := range i.Vaildtypes {
310350

311351
if ret != None && (ck == ret || ck == IPAny) {
312352

@@ -430,7 +470,7 @@ const (
430470

431471
// Requires a string to be without invisible characters
432472
type PureText struct {
433-
mode int
473+
Mode int
434474
}
435475

436476
func ValidPureText(m int) PureText {
@@ -535,7 +575,7 @@ func (p PureText) IsSatisfied(obj interface{}) bool {
535575
if str, ok := obj.(string); ok {
536576

537577
var ret bool
538-
switch p.mode {
578+
switch p.Mode {
539579
case STRICT:
540580
ret, _ = isPureTextStrict(str)
541581
case NORMAL:
@@ -564,7 +604,7 @@ var checkDenyRelativePath = regexp.MustCompile(`(?m)(` + regexDenyFileNameCharLi
564604

565605
// Requires an string to be sanitary file path
566606
type FilePath struct {
567-
mode int
607+
Mode int
568608
}
569609

570610
func ValidFilePath(m int) FilePath {
@@ -580,7 +620,7 @@ func (f FilePath) IsSatisfied(obj interface{}) bool {
580620
if str, ok := obj.(string); ok {
581621

582622
var ret bool
583-
switch f.mode {
623+
switch f.Mode {
584624

585625
case ALLOW_RELATIVE_PATH:
586626
ret = checkAllowRelativePath.MatchString(str)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy