-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalidator_string.go
196 lines (168 loc) · 4.54 KB
/
validator_string.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package validator
import (
"fmt"
"net"
"net/url"
"strings"
"unicode/utf8"
)
// ValidateBetweenString is
func ValidateBetweenString(v string, left int64, right int64) bool {
return ValidateDigitsBetweenInt64(int64(utf8.RuneCountInString(v)), left, right)
}
// InString check if string str is a member of the set of strings params
func InString(str string, params []string) bool {
for _, param := range params {
if str == param {
return true
}
}
return false
}
// compareString determine if a comparison passes between the given values.
func compareString(first string, second int64, operator string) bool {
switch operator {
case "<":
return int64(utf8.RuneCountInString(first)) < second
case ">":
return int64(utf8.RuneCountInString(first)) > second
case "<=":
return int64(utf8.RuneCountInString(first)) <= second
case ">=":
return int64(utf8.RuneCountInString(first)) >= second
case "==":
return int64(utf8.RuneCountInString(first)) == second
default:
panic(fmt.Sprintf("validator: compareString unsupport operator %s", operator))
}
}
// IsNumeric check if the string must be numeric. Empty string is valid.
func IsNumeric(str string) bool {
if IsNull(str) {
return true
}
return rxNumeric.MatchString(str)
}
// IsInt check if the string must be an integer. Empty string is valid.
func IsInt(str string) bool {
if IsNull(str) {
return true
}
return rxInt.MatchString(str)
}
// IsFloat check if the string must be an float. Empty string is valid.
func IsFloat(str string) bool {
if IsNull(str) {
return true
}
return rxFloat.MatchString(str)
}
// IsNull check if the string is null.
func IsNull(str string) bool {
return len(str) == 0
}
// ValidateEmail check if the string is an email.
func ValidateEmail(str string) bool {
// TODO uppercase letters are not supported
return rxEmail.MatchString(str)
}
// ValidateAlpha check if the string may be only contains letters (a-zA-Z). Empty string is valid.
func ValidateAlpha(str string) bool {
if IsNull(str) {
return true
}
return rxAlpha.MatchString(str)
}
// ValidateAlphaNum check if the string may be only contains letters and numbers. Empty string is valid.
func ValidateAlphaNum(str string) bool {
if IsNull(str) {
return true
}
return rxAlphaNum.MatchString(str)
}
// ValidateAlphaDash check if the string may be only contains letters, numbers, dashes and underscores. Empty string is valid.
func ValidateAlphaDash(str string) bool {
if IsNull(str) {
return true
}
return rxAlphaDash.MatchString(str)
}
// ValidateAlphaUnicode check if the string may be only contains letters (a-zA-Z). Empty string is valid.
func ValidateAlphaUnicode(str string) bool {
if IsNull(str) {
return true
}
return rxAlphaUnicode.MatchString(str)
}
// ValidateAlphaNumUnicode check if the string may be only contains letters and numbers. Empty string is valid.
func ValidateAlphaNumUnicode(str string) bool {
if IsNull(str) {
return true
}
return rxAlphaNumUnicode.MatchString(str)
}
// ValidateAlphaDashUnicode check if the string may be only contains letters, numbers, dashes and underscores. Empty string is valid.
func ValidateAlphaDashUnicode(str string) bool {
if IsNull(str) {
return true
}
return rxAlphaDashUnicode.MatchString(str)
}
// ValidateIP check if the string is an ip address.
func ValidateIP(v string) bool {
ip := net.ParseIP(v)
return ip != nil
}
// ValidateIPv4 check if the string is an ipv4 address.
func ValidateIPv4(v string) bool {
ip := net.ParseIP(v)
return ip != nil && ip.To4() != nil
}
// ValidateIPv6 check if the string is an ipv6 address.
func ValidateIPv6(v string) bool {
ip := net.ParseIP(v)
return ip != nil && ip.To4() == nil
}
// ValidateUUID3 check if the string is an uuid3.
func ValidateUUID3(str string) bool {
if IsNull(str) {
return true
}
return rxUUID3.MatchString(str)
}
// ValidateUUID4 check if the string is an uuid4.
func ValidateUUID4(str string) bool {
if IsNull(str) {
return true
}
return rxUUID4.MatchString(str)
}
// ValidateUUID5 check if the string is an uuid5.
func ValidateUUID5(str string) bool {
if IsNull(str) {
return true
}
return rxUUID5.MatchString(str)
}
// ValidateUUID check if the string is an uuid.
func ValidateUUID(str string) bool {
if IsNull(str) {
return true
}
return rxUUID.MatchString(str)
}
// ValidateURL check if the string is an URL.
func ValidateURL(str string) bool {
var i int
if IsNull(str) {
return true
}
if i = strings.Index(str, "#"); i > -1 {
str = str[:i]
}
url, err := url.ParseRequestURI(str)
if err != nil || url.Scheme == "" {
return false
}
return true
}