@@ -19,13 +19,34 @@ import (
19
19
"encoding/base64"
20
20
"fmt"
21
21
"regexp"
22
+ "strings"
22
23
23
24
"github.com/asaskevich/govalidator"
24
25
"github.com/mailru/easyjson/jlexer"
25
26
"github.com/mailru/easyjson/jwriter"
26
27
)
27
28
28
29
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])?)*$`
29
50
// UUIDPattern Regex for UUID that allows uppercase
30
51
UUIDPattern = `(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`
31
52
// UUID3Pattern Regex for UUID3 that allows uppercase
@@ -37,12 +58,35 @@ const (
37
58
)
38
59
39
60
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 )
44
66
)
45
67
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
+
46
90
// IsUUID returns true is the string matches a UUID, upper case is allowed
47
91
func IsUUID (str string ) bool {
48
92
return rxUUID .MatchString (str )
@@ -71,7 +115,7 @@ func init() {
71
115
Default .Add ("email" , & eml , govalidator .IsEmail )
72
116
73
117
hn := Hostname ("" )
74
- Default .Add ("hostname" , & hn , govalidator . IsDNSName )
118
+ Default .Add ("hostname" , & hn , IsHostname )
75
119
76
120
ip4 := IPv4 ("" )
77
121
Default .Add ("ipv4" , & ip4 , govalidator .IsIPv4 )
0 commit comments