-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathvalidators_string.go
More file actions
299 lines (235 loc) · 8.27 KB
/
validators_string.go
File metadata and controls
299 lines (235 loc) · 8.27 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package validate
import (
"encoding/json"
"net"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/gookit/goutil/jsonutil"
"github.com/gookit/goutil/strutil"
"github.com/gookit/validate/v2/internal/reflectx"
)
// ActiveURLTimeout is the per-request timeout used by IsActiveURL when probing
// a URL for reachability. Adjust it to tune how long to wait for the remote
// server before treating the URL as inactive.
var ActiveURLTimeout = 5 * time.Second
/*************************************************************
* region global: string validators
*************************************************************/
// HasWhitespace check. eg "10"
func HasWhitespace(s string) bool {
return s != "" && strings.ContainsRune(s, ' ')
}
// IsIntString check. eg "10"
func IsIntString(s string) bool { return s != "" && rxInt.MatchString(s) }
// IsASCII string.
func IsASCII(s string) bool { return s != "" && rxASCII.MatchString(s) }
// IsPrintableASCII string.
func IsPrintableASCII(s string) bool {
return s != "" && rxPrintableASCII.MatchString(s)
}
// IsBase64 string.
func IsBase64(s string) bool { return s != "" && rxBase64.MatchString(s) }
// IsLatitude string.
func IsLatitude(s string) bool { return s != "" && rxLatitude.MatchString(s) }
// IsLongitude string.
func IsLongitude(s string) bool { return s != "" && rxLongitude.MatchString(s) }
// IsDNSName string.
func IsDNSName(s string) bool { return s != "" && rxDNSName.MatchString(s) }
// HasURLSchema string.
func HasURLSchema(s string) bool { return s != "" && rxURLSchema.MatchString(s) }
// IsFullURL string.
func IsFullURL(s string) bool { return s != "" && rxFullURL.MatchString(s) }
// IsURL string. This is a loose URI-reference check (relative refs, paths and
// bare hosts are accepted); for a strict absolute URL use IsFullURL.
func IsURL(s string) bool {
if s == "" {
return false
}
// a URL/URI reference cannot contain raw whitespace; url.Parse is otherwise
// lenient enough to accept things like "not a url" (#138).
if strings.ContainsAny(s, " \t\r\n\f\v") {
return false
}
_, err := url.Parse(s)
return err == nil
}
// IsActiveURL reports whether s is a reachable/active URL.
//
// It first does a cheap structural check via IsFullURL (must have scheme+host);
// an invalid full URL returns false WITHOUT any network access. Otherwise it
// issues a real HTTP request: a HEAD first, falling back to GET when the server
// answers 405 (Method Not Allowed). A final status code < 400 (2xx/3xx, after
// following the default redirects) is considered active and returns true; any
// transport error or status code >= 400 returns false.
//
// NOTE: this performs a real, network-dependent HTTP request, so it may be slow.
// Tune the timeout via the package-level ActiveURLTimeout. When validating
// untrusted input, be aware of the SSRF risk (an attacker-controlled URL causes
// your server to make outbound requests) and gate it accordingly.
func IsActiveURL(s string) bool {
if s == "" {
return false
}
// cheap short-circuit: not a structurally valid full URL -> never dial out.
if !IsFullURL(s) {
return false
}
client := &http.Client{Timeout: ActiveURLTimeout}
resp, err := client.Head(s)
if err != nil {
return false
}
_ = resp.Body.Close()
// some servers reject HEAD with 405; retry once with GET before judging.
if resp.StatusCode == http.StatusMethodNotAllowed {
resp, err = client.Get(s)
if err != nil {
return false
}
_ = resp.Body.Close()
}
// 2xx/3xx (after redirects) => active and reachable.
return resp.StatusCode < 400
}
// IsDataURI string.
//
// data:[<mime type>] ( [;charset=<charset>] ) [;base64],码内容
// eg. "data:image/gif;base64,R0lGODlhA..."
func IsDataURI(s string) bool { return s != "" && rxDataURI.MatchString(s) }
// IsMultiByte string.
func IsMultiByte(s string) bool { return s != "" && rxMultiByte.MatchString(s) }
// IsISBN10 string.
func IsISBN10(s string) bool { return s != "" && rxISBN10.MatchString(s) }
// IsISBN13 string.
func IsISBN13(s string) bool { return s != "" && rxISBN13.MatchString(s) }
// IsHexadecimal string.
func IsHexadecimal(s string) bool { return s != "" && rxHexadecimal.MatchString(s) }
// IsCnMobile string.
func IsCnMobile(s string) bool { return s != "" && rxCnMobile.MatchString(s) }
// IsHexColor string.
func IsHexColor(s string) bool { return s != "" && rxHexColor.MatchString(s) }
// IsRGBColor string.
func IsRGBColor(s string) bool { return s != "" && rxRGBColor.MatchString(s) }
// IsAlpha string.
func IsAlpha(s string) bool { return s != "" && rxAlpha.MatchString(s) }
// IsAlphaNum string.
func IsAlphaNum(s string) bool { return s != "" && rxAlphaNum.MatchString(s) }
// IsAlphaDash string.
func IsAlphaDash(s string) bool { return s != "" && rxAlphaDash.MatchString(s) }
// IsNumber string. should >= 0
func IsNumber(v any) bool {
v = reflectx.IndirectValue(v)
if v == nil {
return false
}
if s, err := strutil.ToString(v); err == nil {
return s != "" && rxNumber.MatchString(s)
}
return false
}
// IsNumeric is string/int number. should >= 0
func IsNumeric(v any) bool {
v = reflectx.IndirectValue(v)
if v == nil {
return false
}
if s, err := strutil.ToString(v); err == nil {
return s != "" && rxNumber.MatchString(s)
}
return false
}
// IsStringNumber is string number. should >= 0
func IsStringNumber(s string) bool { return s != "" && rxNumber.MatchString(s) }
// IsEmail check
func IsEmail(s string) bool { return s != "" && rxEmail.MatchString(s) }
// IsUUID string
func IsUUID(s string) bool { return s != "" && rxUUID.MatchString(s) }
// IsUUID3 string
func IsUUID3(s string) bool { return s != "" && rxUUID3.MatchString(s) }
// IsUUID4 string
func IsUUID4(s string) bool { return s != "" && rxUUID4.MatchString(s) }
// IsUUID5 string
func IsUUID5(s string) bool { return s != "" && rxUUID5.MatchString(s) }
// IsIP is the validation function for validating if the field's value is a valid v4 or v6 IP address.
func IsIP(s string) bool { return s != "" && net.ParseIP(s) != nil }
// IsIPv4 is the validation function for validating if a value is a valid v4 IP address.
func IsIPv4(s string) bool {
if s == "" {
return false
}
ip := net.ParseIP(s)
return ip != nil && ip.To4() != nil
}
// IsIPv6 is the validation function for validating if the field's value is a valid v6 IP address.
func IsIPv6(s string) bool {
ip := net.ParseIP(s)
return ip != nil && ip.To4() == nil
}
// IsMAC is the validation function for validating if the field's value is a valid MAC address.
func IsMAC(s string) bool {
if s == "" {
return false
}
_, err := net.ParseMAC(s)
return err == nil
}
// IsCIDRv4 is the validation function for validating if the field's value is a valid v4 CIDR address.
func IsCIDRv4(s string) bool {
if s == "" {
return false
}
ip, _, err := net.ParseCIDR(s)
return err == nil && ip.To4() != nil
}
// IsCIDRv6 is the validation function for validating if the field's value is a valid v6 CIDR address.
func IsCIDRv6(s string) bool {
if s == "" {
return false
}
ip, _, err := net.ParseCIDR(s)
return err == nil && ip.To4() == nil
}
// IsCIDR is the validation function for validating if the field's value is a valid v4 or v6 CIDR address.
func IsCIDR(s string) bool {
if s == "" {
return false
}
_, _, err := net.ParseCIDR(s)
return err == nil
}
// IsJSON check if the string is valid JSON (note: uses json.Unmarshal).
func IsJSON(s string) bool {
if s == "" {
return false
}
if !jsonutil.IsJSONFast(s) {
return false
}
var js json.RawMessage
return Unmarshal([]byte(s), &js) == nil
}
// HasLowerCase check string has lower case
func HasLowerCase(s string) bool {
if s == "" {
return false
}
return rxHasLowerCase.MatchString(s)
}
// HasUpperCase check string has upper case
func HasUpperCase(s string) bool {
return s != "" && rxHasUpperCase.MatchString(s)
}
// StartsWith check string is starts with sub-string
func StartsWith(s, sub string) bool { return s != "" && strings.HasPrefix(s, sub) }
// EndsWith check string is ends with sub-string
func EndsWith(s, sub string) bool { return s != "" && strings.HasSuffix(s, sub) }
// StringContains check string is containing sub-string
func StringContains(s, sub string) bool { return s != "" && strings.Contains(s, sub) }
// Regexp match value string
func Regexp(str string, pattern string) bool {
ok, _ := regexp.MatchString(pattern, str)
return ok
}