Skip to content

Commit 2c68ddd

Browse files
meydjerqiangxue
authored andcommitted
Added Domain validation (go-ozzo#43)
* Add Subdomain validation * Added Domain validation README update
1 parent 01aa400 commit 2c68ddd

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ Below is the whole list of the rules provided by the `is` package:
443443
* `IP`: validates if a string is a valid IP address (either version 4 or 6)
444444
* `IPv4`: validates if a string is a valid version 4 IP address
445445
* `IPv6`: validates if a string is a valid version 6 IP address
446+
* `Subdomain`: validates if a string is valid subdomain
447+
* `Domain`: validates if a string is valid domain
446448
* `DNSName`: validates if a string is valid DNS name
447449
* `Host`: validates if a string is a valid IP (both v4 and v6) or a valid DNS name
448450
* `Port`: validates if a string is a valid port number

is/rules.go

+17
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ var (
100100
IPv6 = validation.NewStringRule(govalidator.IsIPv6, "must be a valid IPv6 address")
101101
// Subdomain validates if a string is valid subdomain
102102
Subdomain = validation.NewStringRule(isSubdomain, "must be a valid subdomain")
103+
// Domain validates if a string is valid domain
104+
Domain = validation.NewStringRule(isDomain, "must be a valid domain")
103105
// DNSName validates if a string is valid DNS name
104106
DNSName = validation.NewStringRule(govalidator.IsDNSName, "must be a valid DNS name")
105107
// Host validates if a string is a valid IP (both v4 and v6) or a valid DNS name
@@ -133,9 +135,24 @@ func isDigit(value string) bool {
133135
}
134136

135137
func isSubdomain(value string) bool {
138+
// Subdomain regex source: https://stackoverflow.com/a/7933253
139+
reSubdomain := regexp.MustCompile(`^[A-Za-z0-9](?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9])?$`)
136140
return reSubdomain.MatchString(value)
137141
}
138142

143+
func isDomain(value string) bool {
144+
if len(value) > 255 {
145+
return false
146+
}
147+
148+
// Domain regex source: https://stackoverflow.com/a/7933253
149+
// Slightly modified: Removed 255 max length validation since Go regex does not
150+
// support lookarounds. More info: https://stackoverflow.com/a/38935027
151+
reDomain := regexp.MustCompile(`^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+(?:[a-z]{1,63}| xn--[a-z0-9]{1,59})$`)
152+
153+
return reDomain.MatchString(value)
154+
}
155+
139156
func isUTFNumeric(value string) bool {
140157
for _, c := range value {
141158
if unicode.IsNumber(c) == false {

is/rules_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func TestAll(t *testing.T) {
3636
{"IPv6", IPv6, "2001:4860:0:2001::68", "74.125.19.99", "must be a valid IPv6 address"},
3737
{"MAC", MAC, "0123.4567.89ab", "74.125.19.99", "must be a valid MAC address"},
3838
{"Subdomain", Subdomain, "example-subdomain", "example.com", "must be a valid subdomain"},
39+
{"Domain", Domain, "example-domain.com", "localhost", "must be a valid domain"},
3940
{"DNSName", DNSName, "example.com", "abc%", "must be a valid DNS name"},
4041
{"Host", Host, "example.com", "abc%", "must be a valid IP address or DNS name"},
4142
{"Port", Port, "123", "99999", "must be a valid port number"},

0 commit comments

Comments
 (0)