idValidator

See source code

Creates a validator for typed record IDs that ensures they follow the correct format with the specified prefix. Record IDs in tldraw follow the pattern "prefix:identifier" where the prefix indicates the record type.

function idValidator<Id extends RecordId<UnknownRecord>>(
  prefix: Id['__type__']['typeName']
): T.Validator<Id>

Example

const shapeIdValidator = idValidator<TLShapeId>('shape')
const validId = shapeIdValidator.validate('shape:abc123') // Returns 'shape:abc123' as TLShapeId

const pageIdValidator = idValidator<TLPageId>('page')
const pageId = pageIdValidator.validate('page:main') // Returns 'page:main' as TLPageId

// This would throw an error:
// shapeIdValidator.validate('page:abc123') // Error: shape ID must start with "shape:"

Parameters

NameDescription

prefix

Id['__type__']['typeName']

The required prefix for the ID (e.g., 'shape', 'page', 'asset')

Returns

T.Validator<Id>

A validator that checks the ID format and returns the typed ID

Prev
getDefaultUserPresence
Next
isBinding