-
Notifications
You must be signed in to change notification settings - Fork 1
/
uuid.go
67 lines (53 loc) · 1.5 KB
/
uuid.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
package gencheck
import (
"fmt"
"regexp"
)
var uuidMatcher = regexp.MustCompile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
var uuid3Matcher = regexp.MustCompile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-3[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
var uuid4Matcher = regexp.MustCompile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$")
var uuid5Matcher = regexp.MustCompile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-5[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$")
// IsUUID validates that the given string is a UUID value
func IsUUID(s *string) error {
if s == nil {
return nil
}
matches := uuidMatcher.MatchString(*s)
if !matches {
return fmt.Errorf("'%s' is not a UUID", *s)
}
return nil
}
// IsUUIDv3 validates that the given string is a UUIDv3 value
func IsUUIDv3(s *string) error {
if s == nil {
return nil
}
matches := uuid3Matcher.MatchString(*s)
if !matches {
return fmt.Errorf("'%s' is not a UUIDv3", *s)
}
return nil
}
// IsUUIDv4 validates that the given string is a UUIDv4 value
func IsUUIDv4(s *string) error {
if s == nil {
return nil
}
matches := uuid4Matcher.MatchString(*s)
if !matches {
return fmt.Errorf("'%s' is not a UUIDv4", *s)
}
return nil
}
// IsUUIDv5 validates that the given string is a UUIDv5 value
func IsUUIDv5(s *string) error {
if s == nil {
return nil
}
matches := uuid5Matcher.MatchString(*s)
if !matches {
return fmt.Errorf("'%s' is not a UUIDv5", *s)
}
return nil
}