forked from aliuygur/is
-
Notifications
You must be signed in to change notification settings - Fork 0
/
is.go
723 lines (595 loc) · 14.1 KB
/
is.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
package is
import (
"encoding/base64"
"encoding/json"
"math"
"net"
"net/url"
"os"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
// InRange returns true if value lies between left and right border
func InRange(value, left, right float64) bool {
if left > right {
left, right = right, left
}
return value >= left && value <= right
}
// Email is a constraint to do a simple validation for email addresses, it only check if the string contains "@"
// and that it is not in the first or last character of the string
// https://en.wikipedia.org/wiki/Email_address#Valid_email_addresses
func Email(s string) bool {
if !strings.Contains(s, "@") || s[0] == '@' || s[len(s)-1] == '@' {
return false
}
return true
}
// URL check if the string is an URL.
func URL(str string) bool {
if str == "" || len(str) >= 2083 || len(str) <= 3 || str[0] == '.' {
return false
}
u, err := url.Parse(str)
if err != nil {
return false
}
if strings.HasPrefix(u.Host, ".") {
return false
}
if u.Host == "" && (u.Path != "" && !strings.Contains(u.Path, ".")) {
return false
}
return rxURL.MatchString(str)
}
// RequestURL check if the string rawurl, assuming
// it was received in an HTTP request, is a valid
// URL confirm to RFC 3986
func RequestURL(rawurl string) bool {
url, err := url.ParseRequestURI(rawurl)
if err != nil {
return false
}
if len(url.Scheme) == 0 {
return false
}
return true
}
// RequestURI check if the string rawurl, assuming
// it was received in an HTTP request, is an
// absolute URI or an absolute path.
func RequestURI(rawurl string) bool {
_, err := url.ParseRequestURI(rawurl)
return err == nil
}
// Alpha check if the string contains only letters (a-zA-Z).
func Alpha(s string) bool {
if len(s) == 0 {
return false
}
for _, v := range s {
if ('Z' < v || v < 'A') && ('z' < v || v < 'a') {
return false
}
}
return true
}
// UTFLetter check if the string contains only unicode letter characters.
// Similar to is.Alpha but for all languages.
func UTFLetter(s string) bool {
if len(s) == 0 {
return false
}
for _, v := range s {
if !unicode.IsLetter(v) {
return false
}
}
return true
}
// Alphanumeric check if the string contains only letters and numbers.
func Alphanumeric(s string) bool {
if len(s) == 0 {
return false
}
for _, v := range s {
if ('Z' < v || v < 'A') && ('z' < v || v < 'a') && ('9' < v || v < '0') {
return false
}
}
return true
}
// UTFLetterNumeric check if the string contains only unicode letters and numbers.
func UTFLetterNumeric(s string) bool {
if len(s) == 0 {
return false
}
for _, v := range s {
if !unicode.IsLetter(v) && !unicode.IsNumber(v) {
return false
}
}
return true
}
// Numeric check if the string contains only numbers.
func Numeric(s string) bool {
if len(s) == 0 {
return false
}
for _, v := range s {
if '9' < v || v < '0' {
return false
}
}
return true
}
// UTFNumeric check if the string contains only unicode numbers of any kind.
// Numbers can be 0-9 but also Fractions ¾,Roman Ⅸ and Hangzhou 〩.
func UTFNumeric(s string) bool {
if len(s) == 0 {
return false
}
for _, v := range s {
if !unicode.IsNumber(v) {
return false
}
}
return true
}
// Whole returns true if value is whole number
func Whole(value float64) bool {
return math.Remainder(value, 1) == 0
}
// Natural returns true if value is natural number (positive and whole)
func Natural(value float64) bool {
return value > 0 && Whole(value)
}
// UTFDigit check if the string contains only unicode radix-10 decimal digits.
func UTFDigit(s string) bool {
if len(s) == 0 {
return false
}
for _, v := range s {
if !unicode.IsDigit(v) {
return false
}
}
return true
}
// Hexadecimal check if the string is a hexadecimal number.
func Hexadecimal(s string) bool {
_, err := strconv.ParseInt(s, 16, 0)
return err == nil
}
// Hexcolor check if the string is a hexadecimal color.
func Hexcolor(s string) bool {
if s == "" {
return false
}
if s[0] == '#' {
s = s[1:]
}
if len(s) != 3 && len(s) != 6 {
return false
}
for _, c := range s {
if ('F' < c || c < 'A') && ('f' < c || c < 'a') && ('9' < c || c < '0') {
return false
}
}
return true
}
// RGBcolor check if the string is a valid RGB color in form rgb(RRR, GGG, BBB).
func RGBcolor(s string) bool {
if s == "" || len(s) < 10 {
return false
}
if s[0:4] != "rgb(" || s[len(s)-1] != ')' {
return false
}
s = s[4 : len(s)-1]
s = strings.TrimSpace(s)
for _, p := range strings.Split(s, ",") {
if len(p) > 1 && p[0] == '0' {
return false
}
p = strings.TrimSpace(p)
if i, e := strconv.Atoi(p); (255 < i || i < 0) || e != nil {
return false
}
}
return true
}
// LowerCase check if the string is lowercase.
func LowerCase(s string) bool {
if len(s) == 0 {
return false
}
return s == strings.ToLower(s)
}
// UpperCase check if the string is uppercase.
func UpperCase(s string) bool {
if len(s) == 0 {
return false
}
return s == strings.ToUpper(s)
}
// Int check if the string is an integer.
func Int(s string) bool {
if len(s) == 0 {
return false
}
_, err := strconv.Atoi(s)
return err == nil
}
// Float check if the string is a float.
func Float(s string) bool {
_, err := strconv.ParseFloat(s, 0)
return err == nil
}
// ByteLength check if the string's length (in bytes) falls in a range.
func ByteLength(s string, min, max int) bool {
return len(s) >= min && len(s) <= max
}
// UUIDv3 check if the string is a UUID version 3.
func UUIDv3(s string) bool {
return UUID(s) && s[14] == '3'
}
// UUIDv4 check if the string is a UUID version 4.
func UUIDv4(s string) bool {
return UUID(s) &&
s[14] == '4' &&
(s[19] == '8' || s[19] == '9' || s[19] == 'a' || s[19] == 'b')
}
// UUIDv5 check if the string is a UUID version 5.
func UUIDv5(s string) bool {
return UUID(s) &&
s[14] == '5' &&
(s[19] == '8' || s[19] == '9' || s[19] == 'a' || s[19] == 'b')
}
// UUID check if the string is a UUID (version 3, 4 or 5).
func UUID(s string) bool {
if len(s) != 36 {
return false
}
for i, c := range s {
if i == 8 || i == 13 || i == 18 || i == 23 {
if c != '-' {
return false
}
continue
}
if ('f' < c || c < 'a') && ('9' < c || c < '0') {
return false
}
}
return true
}
// JSON check if the string is valid JSON (note: uses json.Unmarshal).
func JSON(str string) bool {
var js json.RawMessage
return json.Unmarshal([]byte(str), &js) == nil
}
// Multibyte check if the string contains one or more multibyte chars.
func Multibyte(s string) bool {
if len(s) == 0 {
return false
}
for _, v := range s {
if v >= utf8.RuneSelf {
return true
}
}
return false
}
// ASCII check if the string contains ASCII chars only.
func ASCII(s string) bool {
if len(s) == 0 {
return false
}
for _, v := range s {
if v >= utf8.RuneSelf {
return false
}
}
return true
}
// PrintableASCII check if the string contains printable ASCII chars only.
func PrintableASCII(s string) bool {
if len(s) == 0 {
return false
}
for _, v := range s {
if v < ' ' || v > '~' {
return false
}
}
return true
}
// FullWidth check if the string contains any full-width chars.
func FullWidth(s string) bool {
if len(s) == 0 {
return false
}
for _, v := range s {
if ('z' < v || v < 'a') && (0x7e < v || v < 20) && (65439 < v || v < 65377) && (65500 < v || v < 65440) && (1048288 < v || v < 65512) {
return true
}
}
return false
}
// HalfWidth check if the string contains any half-width chars.
func HalfWidth(s string) bool {
if len(s) == 0 {
return false
}
for _, v := range s {
if (v >= 'a' && v <= 'z') || (v >= 20 && v <= 0x7e) || (v >= 65377 && v <= 65439) || (v >= 65440 && v <= 65500) || (v >= 65512 && v <= 1048288) {
return true
}
}
return false
}
// VariableWidth check if the string contains a mixture of full and half-width chars.
func VariableWidth(s string) bool {
if len(s) == 0 {
return false
}
return HalfWidth(s) && FullWidth(s)
}
// Base64 check if a string is base64 encoded.
func Base64(s string) bool {
if len(s) == 0 {
return false
}
_, err := base64.StdEncoding.DecodeString(s)
return err == nil
}
// FilePath check is a string is Win or Unix file path and returns it's type.
func FilePath(str string) (bool, int) {
if rxWinPath.MatchString(str) {
// check windows path limit see:
// http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#maxpath
if len(str[3:]) > 32767 {
return false, Win
}
return true, Win
} else if rxUnixPath.MatchString(str) {
return true, Unix
}
return false, Unknown
}
// DataURI checks if a string is base64 encoded data URI such as an image
func DataURI(s string) bool {
if len(s) == 0 {
return false
}
if s[:5] != "data:" {
return false
}
var ci int
for i := range s {
if s[i] == ',' {
ci = i
break
}
}
if s[ci-7:ci] != ";base64" {
return false
}
return Base64(s[ci+1:])
}
// ISO3166Alpha2 checks if a string is valid two-letter country code
func ISO3166Alpha2(str string) bool {
for _, entry := range ISO3166List {
if str == entry.Alpha2Code {
return true
}
}
return false
}
// ISO3166Alpha3 checks if a string is valid three-letter country code
func ISO3166Alpha3(str string) bool {
for _, entry := range ISO3166List {
if str == entry.Alpha3Code {
return true
}
}
return false
}
// DNSName will validate the given string as a DNS name
func DNSName(str string) bool {
if str == "" || len(strings.Replace(str, ".", "", -1)) > 255 {
// constraints already violated
return false
}
return rxDNSName.MatchString(str)
}
// DialString validates the given string for usage with the various Dial() functions
func DialString(str string) bool {
if h, p, err := net.SplitHostPort(str); err == nil && h != "" && p != "" && (DNSName(h) || IP(h)) && Port(p) {
return true
}
return false
}
// IP checks if a string is either IP version 4 or 6.
func IP(str string) bool {
return net.ParseIP(str) != nil
}
// Port checks if a string represents a valid port
func Port(str string) bool {
if i, err := strconv.Atoi(str); err == nil && i > 0 && i < 65536 {
return true
}
return false
}
// IPv4 check if the string is an IP version 4.
func IPv4(str string) bool {
ip := net.ParseIP(str)
return ip != nil && strings.Contains(str, ".")
}
// IPv6 check if the string is an IP version 6.
func IPv6(str string) bool {
ip := net.ParseIP(str)
return ip != nil && strings.Contains(str, ":")
}
// MAC check if a string is valid MAC address.
// Possible MAC formats:
// 01:23:45:67:89:ab
// 01:23:45:67:89:ab:cd:ef
// 01-23-45-67-89-ab
// 01-23-45-67-89-ab-cd-ef
// 0123.4567.89ab
// 0123.4567.89ab.cdef
func MAC(str string) bool {
_, err := net.ParseMAC(str)
return err == nil
}
// MongoID check if the string is a valid hex-encoded representation of a MongoDB ObjectId.
func MongoID(str string) bool {
if str == "" || len(str) != 24 {
return false
}
for _, c := range str {
if ('F' < c || c < 'A') && ('f' < c || c < 'a') && ('9' < c || c < '0') {
return false
}
}
return true
}
// Latitude check if a string is valid latitude.
func Latitude(str string) bool {
if str == "" {
return false
}
f, err := strconv.ParseFloat(str, 64)
if err != nil {
return false
}
if 90.0 < f || f < -90.0 {
return false
}
return true
}
// Longitude check if a string is valid longitude.
func Longitude(str string) bool {
if str == "" {
return false
}
f, err := strconv.ParseFloat(str, 64)
if err != nil {
return false
}
if 180.0 < f || f < -180.0 {
return false
}
return true
}
// SSN will validate the given string as a U.S. Social Security Number
// See: http://stackoverflow.com/a/1517044
func SSN(s string) bool {
if len(s) != 9 && len(s) != 11 {
return false
}
s = stripNonNumeric(s)
if len(s) != 9 {
return false
}
if s[:3] == "000" || s[:3] == "666" || s[3:5] == "00" || s[5:] == "0000" {
return false
}
p, err := strconv.ParseInt(s[:3], 10, 0)
if err != nil {
return false
}
if 900 <= p && p <= 999 {
return false
}
return true
}
// Semver check if string is valid semantic version
func Semver(s string) bool {
// minimal valid string is "0.0.1"
if len(s) < 5 {
return false
}
// invalid special chars sequence
if strings.Contains(s, "+-") || strings.Contains(s, "-+") {
return false
}
// strip leading "v"
if s[0] == 'v' {
s = s[1:]
}
// get clean major, clean minor and maybe dirty patch parts
p := strings.SplitN(s, ".", 3)
if len(p) != 3 {
return false
}
// try to extract meta part from patch first
p = append(p[:2], strings.SplitN(p[2], "+", 2)...)
if len(p) == 3 {
// no meta - try to extract pre-release
p = append(p[:2], strings.SplitN(p[2], "-", 2)...)
} else {
// try to extract pre-release and move meta to the end
pr := strings.SplitN(p[2], "-", 2)
if len(pr) == 2 {
p = append(p[:2], pr[0], pr[1], p[3])
}
}
// check that major does not have leading zero
if len(p[0]) > 1 && p[0][0] == '0' {
return false
}
// check that minor does not have leading zero
if len(p[1]) > 1 && p[1][0] == '0' {
return false
}
// check that patch does not have leading zero
if len(p[2]) > 1 && p[2][0] == '0' {
return false
}
// check that major, minor and patch are ints
if !Int(p[0]) || !Int(p[1]) || !Int(p[2]) {
return false
}
// if pre-release or meta exists - check them
if len(p) > 3 {
for i, sp := range p[3:] {
// neither pre-release nor meta parts must not have characters except [0-9a-zA-Z-]
for _, v := range sp {
if ('Z' < v || v < 'A') && ('z' < v || v < 'a') && ('9' < v || v < '0') && v != '.' && v != '-' {
return false
}
}
// pre-release part must not have leading zeroes in numeric parts
if i == 0 {
for _, psp := range strings.Split(sp, ".") {
if len(psp) > 1 && psp[0] == '0' && (psp[1] >= '0' && psp[1] <= '9') {
return false
}
}
}
}
}
return true
}
// StringLength check string's length (including multi byte strings)
func StringLength(str string, min int, max int) bool {
slen := utf8.RuneCountInString(str)
return slen >= min && slen <= max
}
// Exists returns whether the given file or directory exists or not
func Exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}