Skip to content

Commit

Permalink
Add hostname_port validator feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravi Terala committed Jan 3, 2020
1 parent fb6c458 commit 893747e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
20 changes: 20 additions & 0 deletions baked_in.go
Expand Up @@ -166,6 +166,7 @@ var (
"html_encoded": isHTMLEncoded,
"url_encoded": isURLEncoded,
"dir": isDir,
"hostname_port": isHostnamePort,
}
)

Expand Down Expand Up @@ -2007,3 +2008,22 @@ func isDir(fl FieldLevel) bool {

panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}

// isHostnamePort validates a <dns>:<port> combination for fields typically used for socket address.
func isHostnamePort(fl FieldLevel) bool {
val := fl.Field().String()
host, port, err := net.SplitHostPort(val)
if err != nil {
return false
}
// Port must be a iny <= 65535.
if portNum, err := strconv.ParseInt(port, 10, 32); err != nil || portNum > 65535 || portNum < 1 {
return false
}

// If host is specified, it should match a DNS name
if host != "" {
return hostnameRegexRFC1123.MatchString(host)
}
return true
}
10 changes: 5 additions & 5 deletions regexes.go
Expand Up @@ -36,11 +36,11 @@ const (
latitudeRegexString = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$"
longitudeRegexString = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$"
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])$`
hostnameRegexStringRFC952 = `^[a-zA-Z][a-zA-Z0-9\-\.]+[a-zA-Z0-9]$` // https://tools.ietf.org/html/rfc952
hostnameRegexStringRFC1123 = `^[a-zA-Z0-9][a-zA-Z0-9\-\.]+[a-zA-Z0-9]$` // accepts hostname starting with a digit https://tools.ietf.org/html/rfc1123
btcAddressRegexString = `^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$` // bitcoin address
btcAddressUpperRegexStringBech32 = `^BC1[02-9AC-HJ-NP-Z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
btcAddressLowerRegexStringBech32 = `^bc1[02-9ac-hj-np-z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
hostnameRegexStringRFC952 = `^[a-zA-Z][a-zA-Z0-9\-\.]+[a-zA-Z0-9]$` // https://tools.ietf.org/html/rfc952
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
btcAddressRegexString = `^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$` // bitcoin address
btcAddressUpperRegexStringBech32 = `^BC1[02-9AC-HJ-NP-Z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
btcAddressLowerRegexStringBech32 = `^bc1[02-9ac-hj-np-z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32
ethAddressRegexString = `^0x[0-9a-fA-F]{40}$`
ethAddressUpperRegexString = `^0x[0-9A-F]{40}$`
ethAddressLowerRegexString = `^0x[0-9a-f]{40}$`
Expand Down
37 changes: 34 additions & 3 deletions validator_test.go
Expand Up @@ -7954,15 +7954,15 @@ func TestHostnameRFC1123Validation(t *testing.T) {

if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d hostname failed Error: %v", i, errs)
t.Fatalf("Hostname: %v failed Error: %v", test, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d hostname failed Error: %v", i, errs)
t.Fatalf("Hostname: %v failed Error: %v", test, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "hostname_rfc1123" {
t.Fatalf("Index: %d hostname failed Error: %v", i, errs)
t.Fatalf("Hostname: %v failed Error: %v", i, errs)
}
}
}
Expand Down Expand Up @@ -9003,3 +9003,34 @@ func TestGetTag(t *testing.T) {
Equal(t, errs, nil)
Equal(t, tag, "mytag")
}

func Test_hostnameport_validator(t *testing.T) {

type Host struct {
Addr string `validate:"hostname_port"`
}

type testInput struct {
data string
expected bool
}
testData := []testInput{
{"bad..domain.name:234", false},
{"extra.dot.com.", false},
{"localhost:1234", true},
{"192.168.1.1:1234", true},
{":1234", true},
{"domain.com:1334", true},
{"this.domain.com:234", true},
{"domain:75000", false},
{"missing.port", false},
}
for _, td := range testData {
h := Host{Addr: td.data}
v := New()
err := v.Struct(h)
if td.expected != (err == nil) {
t.Fatalf("Test failed for data: %v Error: %v", td.data, err)
}
}
}

0 comments on commit 893747e

Please sign in to comment.