string_formats

AT Protocol string format validation.

atproto_client.models.string_formats.only_validate_if_strict(validate_fn: Callable[[...], str]) Callable[[...], str]

Skip pydantic validation if not opting into strict validation via context.

atproto_client.models.string_formats.validate_at_uri(v: str, _: ValidationInfo) str

Validate an AT Protocol URI.

Must follow pattern:

  • Starts with at://

  • Contains handle or DID

  • Optional /collection/record-key path

  • Max 8KB length

  • No query parameters or fragments

Parameters:

v – The AT-URI to validate (e.g. at://alice.bsky.social/app.bsky.feed.post/3jxtb5w2hkt2m)

Returns:

The validated AT-URI

Raises:

ValueError – If AT-URI format is invalid

atproto_client.models.string_formats.validate_cid(v: str, _: ValidationInfo) str

Validate a Content Identifier (CID).

Must be:

  • Minimum 8 characters

  • Alphanumeric characters and plus signs only

Parameters:

v – The CID to validate (e.g. bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi)

Returns:

The validated CID

Raises:

ValueError – If CID format is invalid

atproto_client.models.string_formats.validate_datetime(v: str, _: ValidationInfo) str

Validate an ISO 8601/RFC 3339 datetime string.

Requirements:

  • Must use uppercase T as time separator

  • Must include seconds (HH:MM:SS)

  • Must have timezone (Z or ±HH:MM)

  • No -00:00 timezone allowed

  • Valid fractional seconds format if used

  • No whitespace allowed

Parameters:

v – The datetime string to validate (e.g. 2024-11-24T06:02:00Z)

Returns:

The validated datetime string

Raises:

ValueError – If datetime format is invalid

atproto_client.models.string_formats.validate_did(v: str, _: ValidationInfo) str

Validate a Decentralized Identifiers (DID).

A DID must follow the pattern:

  • Format: did:method:identifier

  • Method must be lowercase letters

  • Identifier allows alphanumeric chars, dots, underscores, hyphens, and percent

  • Max 2KB length

  • No /?#[]@ characters allowed

  • Valid percent-encoding if used

Parameters:

v – The DID to validate (e.g. did:plc:z72i7hdynmk6r22z27h6tvur)

Returns:

The validated DID

Raises:

ValueError – If DID format is invalid

atproto_client.models.string_formats.validate_handle(v: str, _: ValidationInfo) str

Validate an AT Protocol Handle Identifier.

A handle must be a valid domain name with:

  • 2+ segments separated by dots

  • ASCII alphanumeric characters and hyphens only

  • 1-63 chars per segment

  • Max 253 chars total

  • Last segment cannot start with a digit

Parameters:

v – The handle to validate (e.g. alice.bsky.social)

Returns:

The validated handle

Raises:

ValueError – If handle format is invalid

atproto_client.models.string_formats.validate_language(v: str, _: ValidationInfo) str

Validate an ISO language code.

Must match pattern:

  • 2-3 letter language code or ‘i’

  • Optional subtag with alphanumeric chars and hyphens

Parameters:

v – The language code to validate (e.g. en or en-US)

Returns:

The validated language code

Raises:

ValueError – If language code format is invalid

atproto_client.models.string_formats.validate_nsid(v: str, _: ValidationInfo) str

Validate an AT Protocol NSID (Namespaced Identifier).

An NSID must have:

  • 3+ segments separated by dots

  • Reversed domain name (lowercase alphanumeric + hyphen)

  • Name segment (letters only)

  • Max 317 chars total

  • No segments ending in numbers

  • No @_*#! special characters

  • Max 63 chars per segment

Parameters:

v – The NSID to validate (e.g. app.bsky.feed.post)

Returns:

The validated NSID

Raises:

ValueError – If NSID format is invalid

atproto_client.models.string_formats.validate_record_key(v: str, _: ValidationInfo) str

Validate an AT Protocol Record Key (rkey).

A record key must:

  • Be 1-512 characters

  • Contain only alphanumeric chars, dots, underscores, colons, tildes, or hyphens

  • Not be “.” or “..”

Parameters:

v – The record key to validate (e.g. 3jxtb5w2hkt2m)

Returns:

The validated record key

Raises:

ValueError – If record key format is invalid

atproto_client.models.string_formats.validate_tid(v: str, _: ValidationInfo) str

Validate an AT Protocol TID (Timestamp Identifiers).

Must be:

  • Exactly 13 characters

  • Only lowercase letters and numbers 2-7

  • First byte’s high bit (0x40) must be 0

Parameters:

v – The TID to validate (e.g. 3jxtb5w2hkt2m)

Returns:

The validated TID

Raises:

ValueError – If TID format is invalid

atproto_client.models.string_formats.validate_uri(v: str, _: ValidationInfo) str

Validate a standard URI.

Requirements:

  • Must have a scheme starting with a letter

  • Must have authority (netloc) or path/query/fragment

  • Max 8KB length

  • No spaces allowed

  • Must follow RFC-3986 format

Parameters:

v – The URI to validate (e.g. https://example.com/path)

Returns:

The validated URI

Raises:

ValueError – If URI format is invalid