MCPcopy
hub / github.com/google/uuid / Validate

Function Validate

uuid.go:223–268  ·  view source on GitHub ↗

Validate 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 invalid, ot

(s string)

Source from the content-addressed store, hash-verified

221// {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
222// It returns an error if the format is invalid, otherwise nil.
223func Validate(s string) error {
224 switch len(s) {
225 // Standard UUID format
226 case 36:
227
228 // UUID with "urn:uuid:" prefix
229 case 36 + 9:
230 if !strings.EqualFold(s[:9], "urn:uuid:") {
231 return URNPrefixError{s[:9]}
232 }
233 s = s[9:]
234
235 // UUID enclosed in braces
236 case 36 + 2:
237 if s[0] != '{' || s[len(s)-1] != '}' {
238 return ErrInvalidBracketedFormat
239 }
240 s = s[1 : len(s)-1]
241
242 // UUID without hyphens
243 case 32:
244 for i := 0; i < len(s); i += 2 {
245 _, ok := xtob(s[i], s[i+1])
246 if !ok {
247 return ErrInvalidUUIDFormat
248 }
249 }
250
251 default:
252 return invalidLengthError{len(s)}
253 }
254
255 // Check for standard UUID format
256 if len(s) == 36 {
257 if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
258 return ErrInvalidUUIDFormat
259 }
260 for _, x := range []int{0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34} {
261 if _, ok := xtob(s[x], s[x+1]); !ok {
262 return ErrInvalidUUIDFormat
263 }
264 }
265 }
266
267 return nil
268}
269
270// String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
271// , or "" if uuid is invalid.

Callers 1

TestValidateFunction · 0.85

Calls 1

xtobFunction · 0.85

Tested by 1

TestValidateFunction · 0.68