directus
1type UUID = `${string}-${string}-${string}-${string}-${string}`;
2
3/**
4* Based on the patterns found in the 'uuid' and 'uuid-validate' npm packages, both of which are MIT licensed.
5*
6* The primary difference between this pattern and the patterns found in the referenced packages is that
7* no validation over the version component (the 14th character) is performed, while the
8* packages fail if the version is not a known one (only versions 1 through 5 are accepted).
9*
10* This specification complies with all major database vendors.
11*
12* e22f209d-9e85-4ef5-b1fe-7dc09d2b67cf
13* ^ version
14*
15* @see https://datatracker.ietf.org/doc/html/rfc4122
16* @see https://github.com/uuidjs/uuid/blob/bc46e198ab06311a9d82d3c9c6222062dd27f760/src/regex.js
17* @see https://github.com/microsoft/uuid-validate/blob/06554db1b093aa6bb429156fa8964e1cde2b750c/index.js
18* @see https://github.com/directus/directus/issues/21573
19*/
20const regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
21
22export function isValidUuid(value: string): value is UUID {
23return regex.test(value);
24}
25