Skip to content

Commit 550287f

Browse files
Dean KarnDean Karn
authored andcommitted
Merge branch 'v1-development' into v1
2 parents 3740ae1 + 6c26ed7 commit 550287f

File tree

5 files changed

+199
-41
lines changed

5 files changed

+199
-41
lines changed

baked_in.go

Lines changed: 82 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,77 @@ package validator
22

33
import (
44
"fmt"
5+
"net/url"
56
"reflect"
67
"strconv"
78
)
89

910
// BakedInValidators is the map of ValidationFunc used internally
1011
// but can be used with any new Validator if desired
1112
var BakedInValidators = map[string]ValidationFunc{
12-
"required": required,
13-
"len": length,
14-
"min": min,
15-
"max": max,
16-
"lt": lt,
17-
"lte": lte,
18-
"gt": gt,
19-
"gte": gte,
20-
"alpha": alpha,
21-
"alphanum": alphanum,
22-
"numeric": numeric,
23-
"number": number,
24-
"hexadecimal": hexadecimal,
25-
"hexcolor": hexcolor,
26-
"rgb": rgb,
27-
"rgba": rgba,
28-
"hsl": hsl,
29-
"hsla": hsla,
30-
"email": email,
13+
"required": hasValue,
14+
"len": hasLengthOf,
15+
"min": hasMinOf,
16+
"max": hasMaxOf,
17+
"lt": isLt,
18+
"lte": isLte,
19+
"gt": isGt,
20+
"gte": isGte,
21+
"alpha": isAlpha,
22+
"alphanum": isAlphanum,
23+
"numeric": isNumeric,
24+
"number": isNumber,
25+
"hexadecimal": isHexadecimal,
26+
"hexcolor": isHexcolor,
27+
"rgb": isRgb,
28+
"rgba": isRgba,
29+
"hsl": isHsl,
30+
"hsla": isHsla,
31+
"email": isEmail,
32+
"url": isURL,
33+
"uri": isURI,
3134
}
3235

33-
func email(field interface{}, param string) bool {
36+
func isURI(field interface{}, param string) bool {
37+
38+
st := reflect.ValueOf(field)
39+
40+
switch st.Kind() {
41+
42+
case reflect.String:
43+
_, err := url.ParseRequestURI(field.(string))
44+
45+
return err == nil
46+
default:
47+
panic(fmt.Sprintf("Bad field type %T", field))
48+
}
49+
}
50+
51+
func isURL(field interface{}, param string) bool {
52+
53+
st := reflect.ValueOf(field)
54+
55+
switch st.Kind() {
56+
57+
case reflect.String:
58+
url, err := url.ParseRequestURI(field.(string))
59+
60+
if err != nil {
61+
return false
62+
}
63+
64+
if len(url.Scheme) == 0 {
65+
return false
66+
}
67+
68+
return err == nil
69+
70+
default:
71+
panic(fmt.Sprintf("Bad field type %T", field))
72+
}
73+
}
74+
75+
func isEmail(field interface{}, param string) bool {
3476

3577
st := reflect.ValueOf(field)
3678

@@ -43,7 +85,7 @@ func email(field interface{}, param string) bool {
4385
}
4486
}
4587

46-
func hsla(field interface{}, param string) bool {
88+
func isHsla(field interface{}, param string) bool {
4789

4890
st := reflect.ValueOf(field)
4991

@@ -56,7 +98,7 @@ func hsla(field interface{}, param string) bool {
5698
}
5799
}
58100

59-
func hsl(field interface{}, param string) bool {
101+
func isHsl(field interface{}, param string) bool {
60102

61103
st := reflect.ValueOf(field)
62104

@@ -69,7 +111,7 @@ func hsl(field interface{}, param string) bool {
69111
}
70112
}
71113

72-
func rgba(field interface{}, param string) bool {
114+
func isRgba(field interface{}, param string) bool {
73115

74116
st := reflect.ValueOf(field)
75117

@@ -82,7 +124,7 @@ func rgba(field interface{}, param string) bool {
82124
}
83125
}
84126

85-
func rgb(field interface{}, param string) bool {
127+
func isRgb(field interface{}, param string) bool {
86128

87129
st := reflect.ValueOf(field)
88130

@@ -95,7 +137,7 @@ func rgb(field interface{}, param string) bool {
95137
}
96138
}
97139

98-
func hexcolor(field interface{}, param string) bool {
140+
func isHexcolor(field interface{}, param string) bool {
99141

100142
st := reflect.ValueOf(field)
101143

@@ -108,7 +150,7 @@ func hexcolor(field interface{}, param string) bool {
108150
}
109151
}
110152

111-
func hexadecimal(field interface{}, param string) bool {
153+
func isHexadecimal(field interface{}, param string) bool {
112154

113155
st := reflect.ValueOf(field)
114156

@@ -121,7 +163,7 @@ func hexadecimal(field interface{}, param string) bool {
121163
}
122164
}
123165

124-
func number(field interface{}, param string) bool {
166+
func isNumber(field interface{}, param string) bool {
125167

126168
st := reflect.ValueOf(field)
127169

@@ -134,7 +176,7 @@ func number(field interface{}, param string) bool {
134176
}
135177
}
136178

137-
func numeric(field interface{}, param string) bool {
179+
func isNumeric(field interface{}, param string) bool {
138180

139181
st := reflect.ValueOf(field)
140182

@@ -147,7 +189,7 @@ func numeric(field interface{}, param string) bool {
147189
}
148190
}
149191

150-
func alphanum(field interface{}, param string) bool {
192+
func isAlphanum(field interface{}, param string) bool {
151193

152194
st := reflect.ValueOf(field)
153195

@@ -160,7 +202,7 @@ func alphanum(field interface{}, param string) bool {
160202
}
161203
}
162204

163-
func alpha(field interface{}, param string) bool {
205+
func isAlpha(field interface{}, param string) bool {
164206

165207
st := reflect.ValueOf(field)
166208

@@ -173,7 +215,7 @@ func alpha(field interface{}, param string) bool {
173215
}
174216
}
175217

176-
func required(field interface{}, param string) bool {
218+
func hasValue(field interface{}, param string) bool {
177219

178220
st := reflect.ValueOf(field)
179221

@@ -187,7 +229,7 @@ func required(field interface{}, param string) bool {
187229
}
188230
}
189231

190-
func gte(field interface{}, param string) bool {
232+
func isGte(field interface{}, param string) bool {
191233

192234
st := reflect.ValueOf(field)
193235

@@ -223,7 +265,7 @@ func gte(field interface{}, param string) bool {
223265
}
224266
}
225267

226-
func gt(field interface{}, param string) bool {
268+
func isGt(field interface{}, param string) bool {
227269

228270
st := reflect.ValueOf(field)
229271

@@ -262,7 +304,7 @@ func gt(field interface{}, param string) bool {
262304
// length tests whether a variable's length is equal to a given
263305
// value. For strings it tests the number of characters whereas
264306
// for maps and slices it tests the number of items.
265-
func length(field interface{}, param string) bool {
307+
func hasLengthOf(field interface{}, param string) bool {
266308

267309
st := reflect.ValueOf(field)
268310

@@ -302,12 +344,12 @@ func length(field interface{}, param string) bool {
302344
// number. For number types, it's a simple lesser-than test; for
303345
// strings it tests the number of characters whereas for maps
304346
// and slices it tests the number of items.
305-
func min(field interface{}, param string) bool {
347+
func hasMinOf(field interface{}, param string) bool {
306348

307-
return gte(field, param)
349+
return isGte(field, param)
308350
}
309351

310-
func lte(field interface{}, param string) bool {
352+
func isLte(field interface{}, param string) bool {
311353

312354
st := reflect.ValueOf(field)
313355

@@ -343,7 +385,7 @@ func lte(field interface{}, param string) bool {
343385
}
344386
}
345387

346-
func lt(field interface{}, param string) bool {
388+
func isLt(field interface{}, param string) bool {
347389

348390
st := reflect.ValueOf(field)
349391

@@ -383,9 +425,9 @@ func lt(field interface{}, param string) bool {
383425
// value. For numbers, it's a simple lesser-than test; for
384426
// strings it tests the number of characters whereas for maps
385427
// and slices it tests the number of items.
386-
func max(field interface{}, param string) bool {
428+
func hasMaxOf(field interface{}, param string) bool {
387429

388-
return lte(field, param)
430+
return isLte(field, param)
389431
}
390432

391433
// asInt retuns the parameter as a int64

doc.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ Here is a list of the current built in validators:
227227
This may not conform to all possibilities of any rfc standard, but neither
228228
does any email provider accept all posibilities...
229229
(Usage: email)
230+
url
231+
This validates that a strings value contains a valid url
232+
This will accept any url the golang request uri accepts but must contain
233+
a schema for example http:// or rtmp://
234+
(Usage: url)
235+
uri
236+
This validates that a strings value contains a valid uri
237+
This will accept any uri the golang request uri accepts (Usage: uri)
230238
231239
Validator notes:
232240

regexes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const (
1414
hslRegexString = "^hsl\\(\\s*(0|[1-9]\\d?|[12]\\d\\d|3[0-5]\\d|360)\\s*,\\s*((0|[1-9]\\d?|100)%)\\s*,\\s*((0|[1-9]\\d?|100)%)\\s*\\)$"
1515
hslaRegexString = "^hsla\\(\\s*(0|[1-9]\\d?|[12]\\d\\d|3[0-5]\\d|360)\\s*,\\s*((0|[1-9]\\d?|100)%)\\s*,\\s*((0|[1-9]\\d?|100)%)\\s*,\\s*((0.[1-9]*)|[01])\\s*\\)$"
1616
emailRegexString = "^(((([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\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zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
17+
// urlRegexString = `^((ftp|http|https):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|((www\.)?)?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?_?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?)|localhost)(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`
18+
// urlRegexString = "^(?:(?:https?|ftp):\\/\\/)(?:\\S+(?::\\S*)?@)?(?:(?!10(?:\\.\\d{1,3}){3})(?!127(?:\\.\\d{1,3}){3})(?!169\\.254(?:\\.\\d{1,3}){2})(?!192\\.168(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\\.(?:[a-z\u00a1-\uffff]{2,})))(?::\\d{2,5})?(?:\\/[^\\s]*)?$"
1719
)
1820

1921
var (

validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (v *Validator) validateFieldByNameAndTag(f interface{}, name string, tag st
267267
return nil
268268
}
269269

270-
if strings.Contains(tag, omitempty) && !required(f, "") {
270+
if strings.Contains(tag, omitempty) && !hasValue(f, "") {
271271
return nil
272272
}
273273

0 commit comments

Comments
 (0)