Exosphere.Lexicon.Schema (Exosphere v0.5.0)

Copy Markdown View Source

The com.atproto.lexicon.schema record type: publishing lexicons as atproto records.

A lexicon document is stored in the com.atproto.lexicon.schema collection with the lexicon's NSID as the record key, giving it the AT-URI at://<did>/com.atproto.lexicon.schema/<nsid>.

This module wraps the document in a struct and validates it against the meta-rules lexicons must follow (beyond the per-node checks Lexicon.Parser already performs): lexicon version 1, an NSID id with no fragment, at most one primary definition per file, record defs carrying a key, and refs not pointing at refs/unions/tokens.

Examples

{:ok, schema} = Exosphere.Lexicon.Schema.new(%{
  "lexicon" => 1,
  "id" => "com.example.post",
  "defs" => %{
    "main" => %{
      "type" => "record",
      "key" => "tid",
      "record" => %{
        "type" => "object",
        "properties" => %{"text" => %{"type" => "string"}}
      }
    }
  }
})

schema.record_key
#=> "com.example.post"

Exosphere.Lexicon.publish(session, pds_url, did, schema)

Summary

Functions

The collection NSID lexicon records live in.

Decode a fetched com.atproto.lexicon.schema record value (the "value" of a com.atproto.repo.getRecord response).

Build a Lexicon.Schema from a lexicon document map (string or atom keys), validating it against the lexicon meta-rules.

The record key for this schema: the lexicon NSID itself.

Encode to the wire record map, including $type.

Validate a raw lexicon document against the lexicon meta-rules.

Types

t()

@type t() :: %Exosphere.Lexicon.Schema{
  defs: %{optional(String.t()) => map()},
  description: String.t() | nil,
  id: String.t(),
  lexicon: 1,
  parsed: Exosphere.Lexicon.Parser.lexicon() | nil
}

Functions

collection()

@spec collection() :: String.t()

The collection NSID lexicon records live in.

from_record(record)

@spec from_record(map()) ::
  {:ok, t()} | {:error, [{path :: String.t(), message :: String.t()}]}

Decode a fetched com.atproto.lexicon.schema record value (the "value" of a com.atproto.repo.getRecord response).

new(attrs)

Build a Lexicon.Schema from a lexicon document map (string or atom keys), validating it against the lexicon meta-rules.

Returns {:ok, schema} or {:error, [{path, message}]}.

record_key(schema)

@spec record_key(t()) :: String.t()

The record key for this schema: the lexicon NSID itself.

to_record(schema)

@spec to_record(t()) :: map()

Encode to the wire record map, including $type.

validate_document(doc)

@spec validate_document(map()) :: [{path :: String.t(), message :: String.t()}]

Validate a raw lexicon document against the lexicon meta-rules.

Lexicon.Parser checks the per-node structure (types, record shape, union refs); this adds the document-level rules: one primary def per lexicon, record defs require a key, refs never target refs/unions/ tokens, and id must be a simple NSID. Returns a list of {path, message} errors (empty when valid).