API reference › @evolu/common › Type › Ulid

const Ulid: brand(
  "Ulid",
  String,
  (value: string) =>
    /^[0-7][0-9A-HJKMNP-TV-Z]{25}$/u.test(value)
      ? ok()
      : err<UlidError>({ type: "Ulid", value }),
  formatUlidError,
);

Defined in: packages/common/src/Type.ts:10765

ULID in its canonical uppercase text form, as defined by the ULID specification.

Ulid accepts 26 characters of Crockford's Base32 alphabet: the digits and the uppercase letters except I, L, O, and U. The characters spell 130 bits, and a ULID has 128, so the first character is at most 7. Every 128-bit value has exactly one Ulid.

The ULID specification reads ULIDs in any case. Ulid requires uppercase so that equal ULIDs are equal strings; uppercase text from other sources before validating it.

Convert a Ulid to an Id with ulidToId and back with idToUlid.

Example

import { assertEqual, assertErr, assertOk, Ulid } from "@evolu/common";

const value = "01ARZ3NDEKTSV4RRFFQ69G5FAV";
assertOk(Ulid.fromUnknown(value), value);
assertErr(Ulid.fromUnknown("81ARZ3NDEKTSV4RRFFQ69G5FAV"));

const lowercase = value.toLowerCase();
const invalid = Ulid.fromUnknown(lowercase);
assertErr(invalid);
assertEqual(invalid.error, { type: "Ulid", value: lowercase });
assertOk(Ulid.fromUnknown(lowercase.toUpperCase()), value);