/
validators.go
171 lines (143 loc) · 6 KB
/
validators.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright The Linux Foundation and each contributor to CommunityBridge.
// SPDX-License-Identifier: MIT
package utils
import (
"fmt"
"regexp"
"strings"
"unicode/utf8"
"github.com/gofrs/uuid"
)
// ValidEmail tests the specified email string, returns true if email is valid, returns false otherwise
func ValidEmail(email string) bool {
emailRegexp := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
return emailRegexp.MatchString(strings.TrimSpace(email))
}
// ValidDomain tests the specified domain string, returns true if domain is valid, returns false otherwise
func ValidDomain(domain string, allowWildcard bool) (string, bool) { // nolint
domain = strings.TrimSpace(domain)
switch {
case len(domain) == 0:
return "domain is empty", false
case len(domain) > 255:
return fmt.Sprintf("domain name length is %d, can't exceed 255", len(domain)), false
}
var l int
for i := 0; i < len(domain); i++ {
b := domain[i]
if b == '.' {
// check domain labels validity
switch {
case i == l:
return fmt.Sprintf("invalid character '%c' at offset %d: label can't begin with a period", b, i), false
case i-l > 63:
return fmt.Sprintf("byte length of label '%s' is %d, can't exceed 63", domain[l:i], i-l), false
case domain[l] == '-':
return fmt.Sprintf("label '%s' at offset %d begins with a hyphen", domain[l:i], l), false
case domain[i-1] == '-':
return fmt.Sprintf("label '%s' at offset %d ends with a hyphen", domain[l:i], l), false
}
l = i + 1
continue
}
// If wildcard domains are allowed, e.g. *.linuxfoundation.org
if allowWildcard {
// test label character validity, note: tests are ordered by decreasing validity frequency
if !(b >= 'a' && b <= 'z' || b >= '0' && b <= '9' || b == '-' || b == '*' || b >= 'A' && b <= 'Z') {
// show the printable unicode character starting at byte offset i
c, _ := utf8.DecodeRuneInString(domain[i:])
if c == utf8.RuneError {
return fmt.Sprintf("invalid character at offset %d", i), false
}
return fmt.Sprintf("invalid character '%c' at offset %d", c, i), false
}
} else {
// test label character validity, note: tests are ordered by decreasing validity frequency
if !(b >= 'a' && b <= 'z' || b >= '0' && b <= '9' || b == '-' || b >= 'A' && b <= 'Z') {
// show the printable unicode character starting at byte offset i
c, _ := utf8.DecodeRuneInString(domain[i:])
if c == utf8.RuneError {
return fmt.Sprintf("invalid character at offset %d", i), false
}
return fmt.Sprintf("invalid character '%c' at offset %d", c, i), false
}
}
}
// check top level domain validity
switch {
case l == len(domain):
return "missing top level domain, domain can't end with a period", false
case len(domain)-l > 63:
return fmt.Sprintf("byte length of top level domain '%s' is %d, can't exceed 63", domain[l:], len(domain)-l), false
case domain[l] == '-':
return fmt.Sprintf("top level domain '%s' at offset %d begins with a hyphen", domain[l:], l), false
case domain[len(domain)-1] == '-':
return fmt.Sprintf("top level domain '%s' at offset %d ends with a hyphen", domain[l:], l), false
case domain[l] >= '0' && domain[l] <= '9':
return fmt.Sprintf("top level domain '%s' at offset %d begins with a digit", domain[l:], l), false
}
return "", true
}
// ValidGitHubUsername tests the specified GitHub username string, returns true if valid, returns false otherwise
func ValidGitHubUsername(githubUsername string) (string, bool) {
if len(strings.TrimSpace(githubUsername)) <= 2 {
return "github username must be 3 or more characters", false
}
// For now, we only allow alphanumeric values
re := regexp.MustCompile("^[a-zA-Z0-9._-]*$")
valid := re.MatchString(strings.TrimSpace(githubUsername))
if !valid {
return fmt.Sprintf("invalid GitHub username: %s", githubUsername), false
}
return "", true
}
// ValidGitlabUsername tests the specified Gitlab username string, returns true if valid, returns false otherwise
func ValidGitlabUsername(gitlabUsername string) (string, bool) {
if len(strings.TrimSpace(gitlabUsername)) <= 2 {
return "gitlab username must be 3 or more characters", false
}
// For now, we only allow alphanumeric values
re := regexp.MustCompile("^[a-zA-Z0-9._-]*$")
valid := re.MatchString(strings.TrimSpace(gitlabUsername))
if !valid {
return fmt.Sprintf("invalid Gitlab username: %s", gitlabUsername), false
}
return "", true
}
// ValidGitHubOrg tests the specified GitHub Organization string, returns true if valid, returns false otherwise
func ValidGitHubOrg(githubOrg string) (string, bool) {
if len(strings.TrimSpace(githubOrg)) <= 2 {
return "github organization must be 3 or more characters", false
}
re := regexp.MustCompile("^[a-zA-Z0-9._-]*$")
valid := re.MatchString(strings.TrimSpace(githubOrg))
if !valid {
return fmt.Sprintf("invalid GitHub organization: %s", githubOrg), false
}
return "", true
}
// ValidGitlabOrg tests the specified Gitlab Organization string, returns true if valid, returns false otherwise
func ValidGitlabOrg(gitlabOrg string) (string, bool) {
if len(strings.TrimSpace(gitlabOrg)) <= 2 {
return "gitlab organization must be 3 or more characters", false
}
re := regexp.MustCompile(`^(?:http(s)?:\/\/)?(?:www\.)?(\w+[\w-]+\w+\.)?gitlab\.com[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]{3,100}$`)
valid := re.MatchString(strings.TrimSpace(gitlabOrg))
if !valid {
return fmt.Sprintf("invalid Gitlab organization: %s", gitlabOrg), false
}
return "", true
}
// IsUUIDv4 returns true if the specified ID is in the UUIDv4 format, otherwise returns false
func IsUUIDv4(id string) bool {
value, err := uuid.FromString(id)
if err != nil {
return false
}
return value.Version() == uuid.V4
}
// IsSalesForceID returns true if the specified ID is a SalesForce formatted ID, otherwise returns false
func IsSalesForceID(id string) bool {
regExp := regexp.MustCompile("^[a-zA-Z0-9]{18}|[a-zA-Z0-9]{15}$")
return regExp.MatchString(strings.TrimSpace(id))
}