Skip to main content

Function: toCID()

toCID(cid): null | CID

Defined in: src/types/atproto-validators.ts:172

Validates and brands a CID string.

Parameters

cid

string

Potential CID string

Returns

null | CID

Branded CID if valid, null otherwise

Remarks

CID format: Base32-encoded multihash with codec prefix

Validation rules:

  • Common prefixes: bafy, bafk, bafm, Qm (CIDv0)
  • Case-sensitive base32 encoding
  • Minimum length for valid CID

This function performs basic format validation. It does not verify that the CID corresponds to retrievable content.

Example

const valid = toCID('bafyreib2rxk3rybk3aobmv5dgudb4vls5sj3bkxfq7c42wgk6b6a7q');
console.log(valid); // 'bafyreib2rxk3rybk3aobmv5dgudb4vls5sj3bkxfq7c42wgk6b6a7q'

const invalid = toCID('not-a-cid');
console.log(invalid); // null