validateUUID returns an error if s is not a properly formatted UUID in one of the following formats: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} It returns an error if the format is i
(s string)
| 951 | // |
| 952 | // It returns an error if the format is invalid, otherwise nil. |
| 953 | func validateUUID(s string) error { |
| 954 | switch len(s) { |
| 955 | // Standard UUID format |
| 956 | case 36: |
| 957 | |
| 958 | // UUID with "urn:uuid:" prefix |
| 959 | case 36 + 9: |
| 960 | if !strings.EqualFold(s[:9], "urn:uuid:") { |
| 961 | return fmt.Errorf("invalid urn prefix: %q", s[:9]) |
| 962 | } |
| 963 | s = s[9:] |
| 964 | |
| 965 | // UUID enclosed in braces |
| 966 | case 36 + 2: |
| 967 | if s[0] != '{' || s[len(s)-1] != '}' { |
| 968 | return errors.New("invalid bracketed UUID format") |
| 969 | } |
| 970 | s = s[1 : len(s)-1] |
| 971 | |
| 972 | // UUID without hyphens |
| 973 | case 32: |
| 974 | for i := 0; i < len(s); i += 2 { |
| 975 | _, ok := xtob(s[i], s[i+1]) |
| 976 | if !ok { |
| 977 | return errors.New("invalid UUID format") |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | default: |
| 982 | return invalidLengthError{len(s)} |
| 983 | } |
| 984 | |
| 985 | // Check for standard UUID format |
| 986 | if len(s) == 36 { |
| 987 | if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { |
| 988 | return errors.New("invalid UUID format") |
| 989 | } |
| 990 | for _, x := range []int{0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34} { |
| 991 | if _, ok := xtob(s[x], s[x+1]); !ok { |
| 992 | return errors.New("invalid UUID format") |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | return nil |
| 998 | } |
no test coverage detected
searching dependent graphs…