Overview
The@atproto/syntax package provides validation and parsing utilities for AT Protocol identifier formats including DIDs, handles, NSIDs, AT URIs, TIDs, and record keys.
Installation
DID (Decentralized Identifier)
Validation for W3C DID identifiers.Validation Functions
void
InvalidDidError if invalid.DID Format
- Must start with
did: - Method name must be lowercase letters
- Method-specific identifier can contain:
a-zA-Z0-9._:%- - Cannot end with
:or% - Maximum length: 2048 characters
Handle
Validation for domain-based handles.Validation Functions
boolean
true if the input is a valid handle.void
InvalidHandleError if invalid.string
boolean
Handle Format
- Valid domain name format (RFC-1035, RFC-3696)
- Segments contain only:
a-zA-Z0-9- - Segments cannot start or end with hyphen
- Each segment: 1-63 characters
- Total length: max 253 characters
- At least 2 segments (e.g.,
user.domain) - TLD must start with letter
- Case-insensitive
.local, .arpa, .invalid, .localhost, .internal, .example, .alt, .onion
Examples:
NSID (Namespaced Identifier)
Validation for Lexicon schema identifiers.NSID Class
NSID
InvalidNsidError if invalid.NSID
boolean
true if the string is a valid NSID.authority: string- Reversed domain (e.g., ‘example.com’)name: string- Method namesegments: readonly string[]- All segments
Validation Functions
NSID Format
- Reversed domain notation + name
- Segments contain:
a-zA-Z0-9- - Cannot start/end with hyphen
- First segment cannot start with digit
- Last segment (name) must be letters/digits only (no hyphens)
- Each segment: 1-63 characters
- Total length: max 317 characters (253 + 1 + 63)
- At least 3 segments
AT URI
Validation and parsing for AT Protocol URIs.AtUri Class
AtUri
AtUri
protocol: string- Always'at:'origin: string-at://{host}host: string- DID or handlehostname: string- Alias for hostpathname: string- Path portioncollection: string- Collection NSIDrkey: string- Record keyhash: string- FragmentsearchParams: URLSearchParams- Query parametersdid: DidString- DID (throws if host is not DID)
toString(): AtUriString- Converts to string
AT URI Format
TID (Timestamp Identifier)
Validation for timestamp-based identifiers.boolean
void
InvalidTidError if invalid.TID Format
- Exactly 13 characters
- Base32-sortable encoding
- First character:
[234567abcdefghij] - Remaining characters:
[234567abcdefghijklmnopqrstuvwxyz]
Record Key
Validation for repository record keys.boolean
void
InvalidRecordKeyError if invalid.Record Key Format
- Allowed characters:
a-zA-Z0-9_~.:- - Length: 1-512 characters
- Cannot be
.or..
Datetime
Validation for ISO 8601 datetime strings.boolean
true if input is a valid ISO 8601 datetime.void
Complete Example
Error Classes
All validation functions throw specific error types:InvalidDidErrorInvalidHandleErrorInvalidNsidErrorInvalidTidErrorInvalidRecordKeyErrorInvalidDatetimeError
Related Packages
- @atproto/identity - DID and handle resolution
- @atproto/lexicon - Schema validation using these identifiers
- @atproto/repo - Repository operations