API reference › @evolu/common › Type › Uuid
const Uuid: brand(
"Uuid",
String,
(value: string) =>
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/u.test(
value,
)
? ok()
: err<UuidError>({ type: "Uuid", value }),
formatUuidError,
);
Defined in: packages/common/src/Type.ts:10521
UUID in its canonical lowercase text form, as defined by RFC 9562.
Uuid accepts 32 lowercase hexadecimal digits in the 8-4-4-4-12 layout, as
produced by crypto.randomUUID(). It accepts every version and variant,
including the Nil and Max UUIDs, so every 128-bit value has exactly one
Uuid.
RFC 9562 reads UUIDs in any case. Uuid requires lowercase so that equal UUIDs are equal strings; lowercase text from other sources before validating it.
Convert a Uuid to an Id with uuidToId and back with idToUuid.
Example
import { assertEqual, assertErr, assertOk, Uuid } from "@evolu/common";
const value = "0190a6f4-8c3e-7b2a-9d41-5e6f7a8b9c0d";
assertOk(Uuid.fromUnknown(value), value);
const uppercase = value.toUpperCase();
const invalid = Uuid.fromUnknown(uppercase);
assertErr(invalid);
assertEqual(invalid.error, { type: "Uuid", value: uppercase });
assertOk(Uuid.fromUnknown(uppercase.toLowerCase()), value);