lex-schema

Validate, parse, and construct AT Protocol data against Lexicon schemas with @atproto/lex.

9.6k|915|Updated Dec 17, 2021
One-click install
npx skills add https://github.com/bluesky-social/atproto --skill lex-schema
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: lex-schema
Source: https://github.com/bluesky-social/atproto/tree/main/.agents/skills/lex-schema
Command: npx skills add https://github.com/bluesky-social/atproto --skill lex-schema

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @atproto/lex, @atproto/lex-schema, @atproto/lex-json.

What problem does it solve?

Working with AT Protocol records and XRPC payloads requires checking that unknown data conforms to Lexicon schemas, and hand-rolled validators or generic tools like Zod do not understand Lexicon semantics such as NSIDs, $type discriminators, CIDs, blobs, and strict datetime rules. This Skill provides the operational knowledge for using the generated ./lexicons/ schema tree and the l builder from @atproto/lex to parse, validate, and construct protocol data correctly.

Core Features & Use Cases

  • Schema validation methods: Choose between $parse, $safeParse, $validate, $safeValidate, $assert, $matches, $isTypeOf, and $build depending on whether you need transformation, narrowing, or cheap $type discrimination.
  • Strict vs lenient modes: Control spec conformance with the strict option for datetime, at-uri, language, and blob constraint handling when reading legacy network data.
  • Hand-declared schemas: Use the l builder (l.object, l.typedObject, l.record, l.query, etc.) for one-off internal shapes or schemas authored before their Lexicon JSON exists.
  • Use Case: When the AppView hydrates stored records, use $matches or $safeValidate instead of $parse so that defaults are not applied and the record still hashes to the CID it was stored under.

Quick Start

Show me how to validate an unknown app.bsky.feed.post record using the generated lexicons schema and handle the failure result.

Frequently Asked Questions about lex-schema

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I validate an AT Protocol record against a Lexicon schema in TypeScript?

Import the generated schema from the ./lexicons/ tree and call a validation method such as app.bsky.feed.post.$safeParse(data). The result is a success object with the parsed value or a LexValidationError carrying an issues list with paths.

What is the difference between $parse and $validate in @atproto/lex?

$parse runs in parse mode and may transform the input, filling withDefault defaults and normalizing bytes. $validate runs the same checks but treats any transformation as a failure, guaranteeing the input already conformed as-is.

When should I use $isTypeOf instead of $matches?

$isTypeOf only compares the $type field, making it cheap for hot .find or .filter loops over already-validated unions. It proves nothing about the rest of the shape, so use $matches when data has not been validated yet.

Does @atproto/lex work with Standard Schema form libraries?

Yes, every schema implements Standard Schema v1 via the ~standard property, so spec-compliant form libraries and validators accept a Lexicon schema directly. Note the adapter always runs in parse mode, so defaults and coercion apply.

Why does $assert throw TypeScript error ts(2775) in generic code?

$assert narrows via an asserts signature, which TypeScript only permits when the call target has an explicit type annotation. In generic code, use $check instead, which performs the same runtime check without narrowing.

When should I use strict: false in Lexicon validation?

Use strict: false when reading legacy network data that predates parts of the spec. Lenient mode relaxes datetime timezone requirements, at-uri record-key validation, language semantics, and blob accept/maxSize constraints.