Skip to content

Commit 0cb3db4

Browse files
committed
revert hostname validator
1 parent 5050127 commit 0cb3db4

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

default.go

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,34 @@ import (
1919
"encoding/base64"
2020
"fmt"
2121
"regexp"
22+
"strings"
2223

2324
"github.com/asaskevich/govalidator"
2425
"github.com/mailru/easyjson/jlexer"
2526
"github.com/mailru/easyjson/jwriter"
2627
)
2728

2829
const (
30+
// HostnamePattern http://json-schema.org/latest/json-schema-validation.html#anchor114
31+
// A string instance is valid against this attribute if it is a valid
32+
// representation for an Internet host name, as defined by RFC 1034, section 3.1 [RFC1034].
33+
// http://tools.ietf.org/html/rfc1034#section-3.5
34+
// <digit> ::= any one of the ten digits 0 through 9
35+
// var digit = /[0-9]/;
36+
// <letter> ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case
37+
// var letter = /[a-zA-Z]/;
38+
// <let-dig> ::= <letter> | <digit>
39+
// var letDig = /[0-9a-zA-Z]/;
40+
// <let-dig-hyp> ::= <let-dig> | "-"
41+
// var letDigHyp = /[-0-9a-zA-Z]/;
42+
// <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
43+
// var ldhStr = /[-0-9a-zA-Z]+/;
44+
// <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
45+
// var label = /[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?/;
46+
// <subdomain> ::= <label> | <subdomain> "." <label>
47+
// var subdomain = /^[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?(\.[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?)*$/;
48+
// <domain> ::= <subdomain> | " "
49+
HostnamePattern = `^[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?(\.[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?)*$`
2950
// UUIDPattern Regex for UUID that allows uppercase
3051
UUIDPattern = `(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`
3152
// UUID3Pattern Regex for UUID3 that allows uppercase
@@ -37,12 +58,35 @@ const (
3758
)
3859

3960
var (
40-
rxUUID = regexp.MustCompile(UUIDPattern)
41-
rxUUID3 = regexp.MustCompile(UUID3Pattern)
42-
rxUUID4 = regexp.MustCompile(UUID4Pattern)
43-
rxUUID5 = regexp.MustCompile(UUID5Pattern)
61+
rxHostname = regexp.MustCompile(HostnamePattern)
62+
rxUUID = regexp.MustCompile(UUIDPattern)
63+
rxUUID3 = regexp.MustCompile(UUID3Pattern)
64+
rxUUID4 = regexp.MustCompile(UUID4Pattern)
65+
rxUUID5 = regexp.MustCompile(UUID5Pattern)
4466
)
4567

68+
// IsHostname returns true when the string is a valid hostname
69+
func IsHostname(str string) bool {
70+
if !rxHostname.MatchString(str) {
71+
return false
72+
}
73+
74+
// the sum of all label octets and label lengths is limited to 255.
75+
if len(str) > 255 {
76+
return false
77+
}
78+
79+
// Each node has a label, which is zero to 63 octets in length
80+
parts := strings.Split(str, ".")
81+
valid := true
82+
for _, p := range parts {
83+
if len(p) > 63 {
84+
valid = false
85+
}
86+
}
87+
return valid
88+
}
89+
4690
// IsUUID returns true is the string matches a UUID, upper case is allowed
4791
func IsUUID(str string) bool {
4892
return rxUUID.MatchString(str)
@@ -71,7 +115,7 @@ func init() {
71115
Default.Add("email", &eml, govalidator.IsEmail)
72116

73117
hn := Hostname("")
74-
Default.Add("hostname", &hn, govalidator.IsDNSName)
118+
Default.Add("hostname", &hn, IsHostname)
75119

76120
ip4 := IPv4("")
77121
Default.Add("ipv4", &ip4, govalidator.IsIPv4)

0 commit comments

Comments
 (0)