|
1
|
|
|
package is |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"strings" |
|
5
|
|
|
|
|
6
|
|
|
"github.com/muonsoft/validation/validate" |
|
7
|
|
|
) |
|
8
|
|
|
|
|
9
|
|
|
var reservedTopLevelDomains = []string{ |
|
10
|
|
|
"example", |
|
11
|
|
|
"invalid", |
|
12
|
|
|
"localhost", |
|
13
|
|
|
"test", |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
// Email is used for simplified validation of an email address. It allows all values |
|
17
|
|
|
// with an "@" symbol in, and a "." in the second host part of the email address. |
|
18
|
|
|
func Email(value string) bool { |
|
19
|
|
|
return looseEmailRegex.MatchString(value) |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
// HTML5Email is used for validation of an email address based on pattern for HTML5 |
|
23
|
|
|
// (see https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address). |
|
24
|
|
|
func HTML5Email(value string) bool { |
|
25
|
|
|
return html5EmailRegex.MatchString(value) |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
// URL is used to check that value is a valid URL string. By default (if no schemas are passed), |
|
29
|
|
|
// the function checks only for the http:// and https:// schemas. Use the schemas argument |
|
30
|
|
|
// to configure the list of expected schemas. If an empty string is passed as a schema, then |
|
31
|
|
|
// URL value may be treated as relative (without schema, e.g. "//example.com"). |
|
32
|
|
|
func URL(value string, schemas ...string) bool { |
|
33
|
|
|
return validate.URL(value, schemas...) == nil |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// IP checks that a value is a valid IP address (IPv4 or IPv6). You can use a list |
|
37
|
|
|
// of restrictions to additionally check for a restricted range of IPs. |
|
38
|
|
|
// See validate.IPRestriction for details. |
|
39
|
|
|
func IP(value string, restrictions ...validate.IPRestriction) bool { |
|
40
|
|
|
return validate.IP(value, restrictions...) == nil |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// IPv4 checks that a value is a valid IPv4 address. You can use a list |
|
44
|
|
|
// of restrictions to additionally check for a restricted range of IPs. |
|
45
|
|
|
// See validate.IPRestriction for details. |
|
46
|
|
|
func IPv4(value string, restrictions ...validate.IPRestriction) bool { |
|
47
|
|
|
return validate.IPv4(value, restrictions...) == nil |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// IPv6 checks that a value is a valid IPv6 address. You can use a list |
|
51
|
|
|
// of restrictions to additionally check for a restricted range of IPs. |
|
52
|
|
|
// See validate.IPRestriction for details. |
|
53
|
|
|
func IPv6(value string, restrictions ...validate.IPRestriction) bool { |
|
54
|
|
|
return validate.IPv6(value, restrictions...) == nil |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Hostname checks that a value is a valid hostname. It checks that each label |
|
58
|
|
|
// within a valid hostname may be no more than 63 octets long. Also, it checks that |
|
59
|
|
|
// the total length of the hostname must not exceed 255 characters. |
|
60
|
|
|
// |
|
61
|
|
|
// See StrictHostname for additional checks. |
|
62
|
|
|
func Hostname(value string) bool { |
|
63
|
|
|
return hostnameRegex.MatchString(value) && len(strings.ReplaceAll(value, ".", "")) <= 255 |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// StrictHostname checks that a value is a valid hostname. Beside checks from Hostname function |
|
67
|
|
|
// it checks that hostname is fully qualified and include its top-level domain name (TLD). |
|
68
|
|
|
// For instance, example.com is valid but example is not. |
|
69
|
|
|
// |
|
70
|
|
|
// Additionally it checks for reserved top-level domains according to RFC 2606 and |
|
71
|
|
|
// that's why hostnames containing them are not considered valid: |
|
72
|
|
|
// .example, .invalid, .localhost, and .test. |
|
73
|
|
|
func StrictHostname(value string) bool { |
|
74
|
|
|
if !Hostname(value) { |
|
75
|
|
|
return false |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
domains := strings.Split(value, ".") |
|
79
|
|
|
if len(domains) < 2 { |
|
80
|
|
|
return false |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
tld := domains[len(domains)-1] |
|
84
|
|
|
for _, reservedTLD := range reservedTopLevelDomains { |
|
85
|
|
|
if tld == reservedTLD { |
|
86
|
|
|
return false |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return true |
|
91
|
|
|
} |
|
92
|
|
|
|