Skip to content

Commit 35aff71

Browse files
joeybloggsjoeybloggs
authored andcommitted
added many new validator + tests + documentation:
ascii printascii multibyte datauri latitude longitude ssn
1 parent 7aa7084 commit 35aff71

File tree

4 files changed

+359
-37
lines changed

4 files changed

+359
-37
lines changed

baked_in.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,62 @@ var BakedInValidators = map[string]Func{
5757
"uuid3": isUUID3,
5858
"uuid4": isUUID4,
5959
"uuid5": isUUID5,
60+
"ascii": isASCII,
61+
"printascii": isPrintableASCII,
62+
"multibyte": hasMultiByteCharacter,
63+
"datauri": isDataURI,
64+
"latitude": isLatitude,
65+
"longitude": isLongitude,
66+
"ssn": isSSN,
67+
}
68+
69+
func isSSN(top interface{}, current interface{}, field interface{}, param string) bool {
70+
71+
if len(field.(string)) != 11 {
72+
return false
73+
}
74+
75+
return matchesRegex(sSNRegex, field)
76+
}
77+
78+
func isLongitude(top interface{}, current interface{}, field interface{}, param string) bool {
79+
return matchesRegex(longitudeRegex, field)
80+
}
81+
82+
func isLatitude(top interface{}, current interface{}, field interface{}, param string) bool {
83+
return matchesRegex(latitudeRegex, field)
84+
}
85+
86+
func isDataURI(top interface{}, current interface{}, field interface{}, param string) bool {
87+
88+
uri := strings.SplitN(field.(string), ",", 2)
89+
90+
if len(uri) != 2 {
91+
return false
92+
}
93+
94+
if !matchesRegex(dataURIRegex, uri[0]) {
95+
return false
96+
}
97+
98+
return isBase64(top, current, uri[1], param)
99+
}
100+
101+
func hasMultiByteCharacter(top interface{}, current interface{}, field interface{}, param string) bool {
102+
103+
if len(field.(string)) == 0 {
104+
return true
105+
}
106+
107+
return matchesRegex(multibyteRegex, field)
108+
}
109+
110+
func isPrintableASCII(top interface{}, current interface{}, field interface{}, param string) bool {
111+
return matchesRegex(printableASCIIRegex, field)
112+
}
113+
114+
func isASCII(top interface{}, current interface{}, field interface{}, param string) bool {
115+
return matchesRegex(aSCIIRegex, field)
60116
}
61117

62118
func isUUID5(top interface{}, current interface{}, field interface{}, param string) bool {

doc.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Here is a list of the current built in validators:
168168
verify it has been assigned.
169169
170170
omitempty
171-
Allows conitional validation, for example if a field is not set with
171+
Allows conditional validation, for example if a field is not set with
172172
a value (Determined by the required validator) then other validation
173173
such as min or max won't run, but if a value is set validation will run.
174174
(Usage: omitempty)
@@ -390,6 +390,38 @@ Here is a list of the current built in validators:
390390
This validates that a string value contains a valid version 5 UUID.
391391
(Usage: uuid5)
392392
393+
ascii
394+
This validates that a string value contains only ASCII characters.
395+
NOTE: if the string is blank, this validates as true.
396+
(Usage: ascii)
397+
398+
asciiprint
399+
This validates that a string value contains only printable ASCII characters.
400+
NOTE: if the string is blank, this validates as true.
401+
(Usage: asciiprint)
402+
403+
multibyte
404+
This validates that a string value contains one or more multibyte characters.
405+
NOTE: if the string is blank, this validates as true.
406+
(Usage: multibyte)
407+
408+
datauri
409+
This validates that a string value contains a valid DataURI.
410+
NOTE: this will also validate that the data portion is valid base64
411+
(Usage: datauri)
412+
413+
latitude
414+
This validates that a string value contains a valid latitude.
415+
(Usage: latitude)
416+
417+
longitude
418+
This validates that a string value contains a valid longitude.
419+
(Usage: longitude)
420+
421+
ssn
422+
This validates that a string value contains a valid U.S. Social Security Number.
423+
(Usage: ssn)
424+
393425
Validator notes:
394426
395427
regex

regexes.go

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,59 @@ package validator
33
import "regexp"
44

55
const (
6-
alphaRegexString = "^[a-zA-Z]+$"
7-
alphaNumericRegexString = "^[a-zA-Z0-9]+$"
8-
numericRegexString = "^[-+]?[0-9]+(?:\\.[0-9]+)?$"
9-
numberRegexString = "^[0-9]+$"
10-
hexadecimalRegexString = "^[0-9a-fA-F]+$"
11-
hexcolorRegexString = "^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$"
12-
rgbRegexString = "^rg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-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
17-
base64RegexString = "(?:^(?:[A-Za-z0-9+\\/]{4}\\n?)*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=)$)"
18-
iSBN10RegexString = "^(?:[0-9]{9}X|[0-9]{10})$"
19-
iSBN13RegexString = "^(?:(?:97(?:8|9))[0-9]{10})$"
20-
uUID3RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$"
21-
uUID4RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
22-
uUID5RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
23-
uUIDRegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
6+
alphaRegexString = "^[a-zA-Z]+$"
7+
alphaNumericRegexString = "^[a-zA-Z0-9]+$"
8+
numericRegexString = "^[-+]?[0-9]+(?:\\.[0-9]+)?$"
9+
numberRegexString = "^[0-9]+$"
10+
hexadecimalRegexString = "^[0-9a-fA-F]+$"
11+
hexcolorRegexString = "^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$"
12+
rgbRegexString = "^rgb\\(\\s*(?:(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s*(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])|(?:0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])%\\s*,\\s*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=|[A-Za-z0-9+\\/]{4})$"
18+
iSBN10RegexString = "^(?:[0-9]{9}X|[0-9]{10})$"
19+
iSBN13RegexString = "^(?:(?:97(?:8|9))[0-9]{10})$"
20+
uUID3RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$"
21+
uUID4RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
22+
uUID5RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
23+
uUIDRegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
24+
aSCIIRegexString = "^[\x00-\x7F]*$"
25+
printableASCIIRegexString = "^[\x20-\x7E]*$"
26+
multibyteRegexString = "[^\x00-\x7F]"
27+
dataURIRegexString = "^data:.+\\/(.+);base64$"
28+
latitudeRegexString = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$"
29+
longitudeRegexString = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$"
30+
sSNRegexString = `^\d{3}[- ]?\d{2}[- ]?\d{4}$`
2431
)
2532

2633
var (
27-
alphaRegex = regexp.MustCompile(alphaRegexString)
28-
alphaNumericRegex = regexp.MustCompile(alphaNumericRegexString)
29-
numericRegex = regexp.MustCompile(numericRegexString)
30-
numberRegex = regexp.MustCompile(numberRegexString)
31-
hexadecimalRegex = regexp.MustCompile(hexadecimalRegexString)
32-
hexcolorRegex = regexp.MustCompile(hexcolorRegexString)
33-
rgbRegex = regexp.MustCompile(rgbRegexString)
34-
rgbaRegex = regexp.MustCompile(rgbaRegexString)
35-
hslRegex = regexp.MustCompile(hslRegexString)
36-
hslaRegex = regexp.MustCompile(hslaRegexString)
37-
emailRegex = regexp.MustCompile(emailRegexString)
38-
base64Regex = regexp.MustCompile(base64RegexString)
39-
iSBN10Regex = regexp.MustCompile(iSBN10RegexString)
40-
iSBN13Regex = regexp.MustCompile(iSBN13RegexString)
41-
uUID3Regex = regexp.MustCompile(uUID3RegexString)
42-
uUID4Regex = regexp.MustCompile(uUID4RegexString)
43-
uUID5Regex = regexp.MustCompile(uUID5RegexString)
44-
uUIDRegex = regexp.MustCompile(uUIDRegexString)
34+
alphaRegex = regexp.MustCompile(alphaRegexString)
35+
alphaNumericRegex = regexp.MustCompile(alphaNumericRegexString)
36+
numericRegex = regexp.MustCompile(numericRegexString)
37+
numberRegex = regexp.MustCompile(numberRegexString)
38+
hexadecimalRegex = regexp.MustCompile(hexadecimalRegexString)
39+
hexcolorRegex = regexp.MustCompile(hexcolorRegexString)
40+
rgbRegex = regexp.MustCompile(rgbRegexString)
41+
rgbaRegex = regexp.MustCompile(rgbaRegexString)
42+
hslRegex = regexp.MustCompile(hslRegexString)
43+
hslaRegex = regexp.MustCompile(hslaRegexString)
44+
emailRegex = regexp.MustCompile(emailRegexString)
45+
base64Regex = regexp.MustCompile(base64RegexString)
46+
iSBN10Regex = regexp.MustCompile(iSBN10RegexString)
47+
iSBN13Regex = regexp.MustCompile(iSBN13RegexString)
48+
uUID3Regex = regexp.MustCompile(uUID3RegexString)
49+
uUID4Regex = regexp.MustCompile(uUID4RegexString)
50+
uUID5Regex = regexp.MustCompile(uUID5RegexString)
51+
uUIDRegex = regexp.MustCompile(uUIDRegexString)
52+
aSCIIRegex = regexp.MustCompile(aSCIIRegexString)
53+
printableASCIIRegex = regexp.MustCompile(printableASCIIRegexString)
54+
multibyteRegex = regexp.MustCompile(multibyteRegexString)
55+
dataURIRegex = regexp.MustCompile(dataURIRegexString)
56+
latitudeRegex = regexp.MustCompile(latitudeRegexString)
57+
longitudeRegex = regexp.MustCompile(longitudeRegexString)
58+
sSNRegex = regexp.MustCompile(sSNRegexString)
4559
)
4660

4761
func matchesRegex(regex *regexp.Regexp, field interface{}) bool {

0 commit comments

Comments
 (0)