Skip to content

Commit

Permalink
revert hostname validator
Browse files Browse the repository at this point in the history
  • Loading branch information
casualjim committed Jan 12, 2017
1 parent 5050127 commit 0cb3db4
Showing 1 changed file with 49 additions and 5 deletions.
54 changes: 49 additions & 5 deletions default.go
Expand Up @@ -19,13 +19,34 @@ import (
"encoding/base64"
"fmt"
"regexp"
"strings"

"github.com/asaskevich/govalidator"
"github.com/mailru/easyjson/jlexer"
"github.com/mailru/easyjson/jwriter"
)

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

var (
rxUUID = regexp.MustCompile(UUIDPattern)
rxUUID3 = regexp.MustCompile(UUID3Pattern)
rxUUID4 = regexp.MustCompile(UUID4Pattern)
rxUUID5 = regexp.MustCompile(UUID5Pattern)
rxHostname = regexp.MustCompile(HostnamePattern)
rxUUID = regexp.MustCompile(UUIDPattern)
rxUUID3 = regexp.MustCompile(UUID3Pattern)
rxUUID4 = regexp.MustCompile(UUID4Pattern)
rxUUID5 = regexp.MustCompile(UUID5Pattern)
)

// IsHostname returns true when the string is a valid hostname
func IsHostname(str string) bool {
if !rxHostname.MatchString(str) {
return false
}

// the sum of all label octets and label lengths is limited to 255.
if len(str) > 255 {
return false
}

// Each node has a label, which is zero to 63 octets in length
parts := strings.Split(str, ".")
valid := true
for _, p := range parts {
if len(p) > 63 {
valid = false
}
}
return valid
}

// IsUUID returns true is the string matches a UUID, upper case is allowed
func IsUUID(str string) bool {
return rxUUID.MatchString(str)
Expand Down Expand Up @@ -71,7 +115,7 @@ func init() {
Default.Add("email", &eml, govalidator.IsEmail)

hn := Hostname("")
Default.Add("hostname", &hn, govalidator.IsDNSName)
Default.Add("hostname", &hn, IsHostname)

ip4 := IPv4("")
Default.Add("ipv4", &ip4, govalidator.IsIPv4)
Expand Down

0 comments on commit 0cb3db4

Please sign in to comment.