Skip to content

Commit 059510c

Browse files
author
Dean Karn
authored
Merge pull request #612 from Giulianos/bugfix/fqdn
Allow numbers in FQDN validation
2 parents ab4e67f + 50166a5 commit 059510c

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

baked_in.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,12 +2002,7 @@ func isFQDN(fl FieldLevel) bool {
20022002
return false
20032003
}
20042004

2005-
if val[len(val)-1] == '.' {
2006-
val = val[0 : len(val)-1]
2007-
}
2008-
2009-
return strings.ContainsAny(val, ".") &&
2010-
hostnameRegexRFC952.MatchString(val)
2005+
return fqdnRegexRFC1123.MatchString(val)
20112006
}
20122007

20132008
// IsDir is the validation function for validating if the current field's value is a valid directory.

regexes.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ const (
3636
latitudeRegexString = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$"
3737
longitudeRegexString = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$"
3838
sSNRegexString = `^[0-9]{3}[ -]?(0[1-9]|[1-9][0-9])[ -]?([1-9][0-9]{3}|[0-9][1-9][0-9]{2}|[0-9]{2}[1-9][0-9]|[0-9]{3}[1-9])$`
39-
hostnameRegexStringRFC952 = `^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$` // https://tools.ietf.org/html/rfc952
40-
hostnameRegexStringRFC1123 = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*?$` // accepts hostname starting with a digit https://tools.ietf.org/html/rfc1123
41-
btcAddressRegexString = `^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$` // bitcoin address
42-
btcAddressUpperRegexStringBech32 = `^BC1[02-9AC-HJ-NP-Z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
43-
btcAddressLowerRegexStringBech32 = `^bc1[02-9ac-hj-np-z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
39+
hostnameRegexStringRFC952 = `^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$` // https://tools.ietf.org/html/rfc952
40+
hostnameRegexStringRFC1123 = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*?$` // accepts hostname starting with a digit https://tools.ietf.org/html/rfc1123
41+
fqdnRegexStringRFC1123 = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{0,62})(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*?(\.[a-zA-Z]{1}[a-zA-Z0-9]{0,62})\.?$` // same as hostnameRegexStringRFC1123 but must contain a non numerical TLD (possibly ending with '.')
42+
btcAddressRegexString = `^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$` // bitcoin address
43+
btcAddressUpperRegexStringBech32 = `^BC1[02-9AC-HJ-NP-Z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
44+
btcAddressLowerRegexStringBech32 = `^bc1[02-9ac-hj-np-z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
4445
ethAddressRegexString = `^0x[0-9a-fA-F]{40}$`
4546
ethAddressUpperRegexString = `^0x[0-9A-F]{40}$`
4647
ethAddressLowerRegexString = `^0x[0-9a-f]{40}$`
@@ -86,6 +87,7 @@ var (
8687
sSNRegex = regexp.MustCompile(sSNRegexString)
8788
hostnameRegexRFC952 = regexp.MustCompile(hostnameRegexStringRFC952)
8889
hostnameRegexRFC1123 = regexp.MustCompile(hostnameRegexStringRFC1123)
90+
fqdnRegexRFC1123 = regexp.MustCompile(fqdnRegexStringRFC1123)
8991
btcAddressRegex = regexp.MustCompile(btcAddressRegexString)
9092
btcUpperAddressRegexBech32 = regexp.MustCompile(btcAddressUpperRegexStringBech32)
9193
btcLowerAddressRegexBech32 = regexp.MustCompile(btcAddressLowerRegexStringBech32)

validator_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8038,6 +8038,8 @@ func TestFQDNValidation(t *testing.T) {
80388038
{"example24.com.", true},
80398039
{"test.example24.com.", true},
80408040
{"test24.example24.com.", true},
8041+
{"24.example24.com", true},
8042+
{"test.24.example.com", true},
80418043
{"test24.example24.com..", false},
80428044
{"example", false},
80438045
{"192.168.0.1", false},

0 commit comments

Comments
 (0)