Add hostname_port validator feature

pull/563/head
Ravi Terala 5 years ago
parent fb6c45823a
commit 893747e5ee
  1. 20
      baked_in.go
  2. 2
      regexes.go
  3. 37
      validator_test.go

@ -166,6 +166,7 @@ var (
"html_encoded": isHTMLEncoded, "html_encoded": isHTMLEncoded,
"url_encoded": isURLEncoded, "url_encoded": isURLEncoded,
"dir": isDir, "dir": isDir,
"hostname_port": isHostnamePort,
} }
) )
@ -2007,3 +2008,22 @@ func isDir(fl FieldLevel) bool {
panic(fmt.Sprintf("Bad field type %T", field.Interface())) 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
}

@ -37,7 +37,7 @@ const (
longitudeRegexString = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$" 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])$` 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 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 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 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 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 btcAddressLowerRegexStringBech32 = `^bc1[02-9ac-hj-np-z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32

@ -7954,15 +7954,15 @@ func TestHostnameRFC1123Validation(t *testing.T) {
if test.expected { if test.expected {
if !IsEqual(errs, nil) { if !IsEqual(errs, nil) {
t.Fatalf("Index: %d hostname failed Error: %v", i, errs) t.Fatalf("Hostname: %v failed Error: %v", test, errs)
} }
} else { } else {
if IsEqual(errs, nil) { if IsEqual(errs, nil) {
t.Fatalf("Index: %d hostname failed Error: %v", i, errs) t.Fatalf("Hostname: %v failed Error: %v", test, errs)
} else { } else {
val := getError(errs, "", "") val := getError(errs, "", "")
if val.Tag() != "hostname_rfc1123" { if val.Tag() != "hostname_rfc1123" {
t.Fatalf("Index: %d hostname failed Error: %v", i, errs) t.Fatalf("Hostname: %v failed Error: %v", i, errs)
} }
} }
} }
@ -9003,3 +9003,34 @@ func TestGetTag(t *testing.T) {
Equal(t, errs, nil) Equal(t, errs, nil)
Equal(t, tag, "mytag") 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)
}
}
}

Loading…
Cancel
Save