idValidator
See source codeCreates 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
Name | Description |
---|---|
|
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
getDefaultUserPresenceNext
isBinding