Block schemas¶
Every content type owns a JSON Schema describing the content_blocks array
its content may hold (AISD-205). cms-api serves that document read-only and
enforces it on admin content writes, so cms-admin, cms-client, and
cms-ai-forge all get the same answer to "what is a valid block?".
Where the schema lives¶
The document is persisted on the content_types.block_schema column in the CMS
database (JSON NOT NULL DEFAULT ('{}'), added by the hq DDL step
0031_add_content_types_block_schema.sql). It is data, not code: an
environment can hold its own document, and changing one does not require a
cms-api deploy.
| Where | What it is |
|---|---|
content_types.block_schema (hq) |
The source of truth SqlAdminStore reads |
src/cms_api/admin/block_schemas.py |
The same documents as constants (SEED_BLOCK_SCHEMAS), used to seed MemoryAdminStore for tests/local and to generate the hq seed |
tests/fixtures/block_schema_seeds.json |
Checked-in {name: document} mapping, so parity is asserted in CI without a sibling hq checkout |
tests/admin/test_block_schemas.py |
Asserts code, fixture, and (when hq is present) the live DDL stay identical |
SqlAdminStore.block_schema_for() memoizes the write-path lookup for 300
seconds (BLOCK_SCHEMA_CACHE_TTL_SECONDS) so a bulk forge run does not add one
query per row. Schemas only change through a DDL step plus a deploy, so that TTL
is also the worst-case staleness after an edit.
{} means accept anything¶
An empty JSON Schema validates every instance. A content type whose
block_schema is {} — because it predates the seed, or was created at runtime
— therefore imposes no constraints, which is the documented soft-migration
behaviour rather than a silent skip. Today homepage_article, trending, and
quiz are seeded; everything else is accept-anything.
Reading a schema¶
{name} may also be the numeric content-type id (/admin/v1/content-types/1/schema);
lookup names are [a-z_], so the two cannot collide. The response is
content_type_id, name, description, is_active, and block_schema. The
route sits inside /admin/v1, so the normal admin JWT gate applies and the forge
service account can read it too.
Public read (frontend)¶
The frontend fetches the same document through the public v2 surface:
curl -sS "$BASE/public/v2/content-types/homepage_article/schema" \
-H "Authorization: Bearer $WORKER_API_KEY"
This read takes only the name (not the numeric id) and returns the raw
block_schema document (not the admin envelope). It 404s on an unknown type
and returns {} for a seeded-but-empty type. It lives under /public/v2, so the
worker-API-key gate applies (no admin JWT); the response is edge-cached long with
an ETag because the document changes only through a DDL step plus a deploy.
Unlike GET /admin/v1/content-types (which filters to active rows for
dropdowns), the schema lookup does not filter on is_active: a
deactivated content type's schema is still readable, and block_schema_for()
still enforces it on writes. This is deliberate — legacy rows of a retired
type must keep validating against the schema they were written under — but
it means is_active: false can appear in this response.
The list endpoint GET /admin/v1/content-types deliberately does not include
block_schema: it feeds dropdowns and a seeded document is roughly 2 KB per row.
The seeded documents¶
Each seeded type owns its own document so it can be tightened independently (the hq DDL writes one full literal per type; there is no shared body stamped by title anymore):
homepage_article— the article-family document below.trending— a distinct document that is structurally identical tohomepage_articletoday (trending is article-family) but is its own builder/literal, so it can diverge in a future DDL step without touching the article schema.quiz— its own, narrower vocabulary (see The quiz contract).
The article-family document (homepage_article / trending):
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "homepage_article content_blocks",
"type": "array",
"items": { "$ref": "#/$defs/block" },
"$defs": {
"block": {
"type": "object",
"required": ["type"],
"properties": {
"type": { "enum": ["paragraph", "text", "heading", "header", "image", "list",
"quote", "blockquote", "delimiter", "checklist_item", "raw",
"richtext"] },
"data": { "type": "object" },
"text": { "type": "string" },
"html": { "type": "string" },
"id": { "type": "string" },
"tunes": { "type": "object" }
},
"additionalProperties": true,
"allOf": [
{ "if": { "properties": { "type": { "enum": ["paragraph", "text", "heading", "header", "quote", "blockquote"] } }, "required": ["type"] },
"then": { "properties": { "data": { "properties": { "text": { "type": "string" } } } } } },
{ "if": { "properties": { "type": { "const": "list" } }, "required": ["type"] },
"then": { "properties": { "data": { "properties": { "items": { "type": "array" } } } } } },
{ "if": { "properties": { "type": { "const": "image" } }, "required": ["type"] },
"then": { "properties": { "data": { "properties": {
"src": { "type": "string" }, "url": { "type": "string" },
"alt": { "type": "string" }, "caption": { "type": "string" },
"file": { "type": "object", "properties": { "url": { "type": "string" } } } } } } } }
]
}
}
}
Block types¶
Aligned with the switch in cms-cf-worker/src/pages/article.ts, so anything
the schema accepts is something the renderer understands.
type |
Constrained shape | Rendered as |
|---|---|---|
paragraph, text |
data.text is a string when present |
Body copy |
heading, header |
data.text is a string when present |
Section title |
quote, blockquote |
data.text is a string when present |
Pull quote |
image |
data.src / data.url / data.alt / data.caption strings, data.file.url string |
Figure |
list |
data.items is an array when present |
Ordered / unordered list |
delimiter |
none | Visual break |
checklist_item |
none beyond the block shape | Listicle slide |
raw |
top-level html is a string when present |
Raw markup |
richtext |
none beyond the block shape | Legacy WYSIWYG copy (114 live rows on stage) |
The quiz contract (v1)¶
quiz is not an article, so it has its own vocabulary
(QUIZ_BLOCK_TYPES in block_schemas.py), deliberately kept out of the
shared CANONICAL_BLOCK_TYPES: the base textual blocks for intro/outro copy
(paragraph, text, heading, header) plus a quiz_question block.
type |
Constrained shape |
|---|---|
paragraph, text, heading, header |
data.text is a string when present |
quiz_question |
data.question (string, required), data.options (array of strings, required, minItems 2), data.answer (integer or string — the index into options or the literal value — optional) |
This is the v1 contract. It is tightenable once quiz rendering exists — for
example, requiring answer or bounding it to a valid option index. Article-only
types (image, list, richtext, …) are rejected for a quiz because they are
absent from the quiz type enum.
Deliberate leniencies¶
The schema is written to accept everything forge and the worker already produce. Each of these is intentional:
- No
minItems. An emptycontent_blocksarray stays valid so an editor can save an empty draft — today's behaviour. additionalProperties: trueeverywhere. Forge and the worker add keys (tunes,page,title, provenance) freely; unknown keys must not fail a write.- Nothing inside
datais required, and empty strings pass. Forge writesimageblocks withdata.src = ""and fills them once the media children finish, so requiring a non-emptysrcwould break the generation pipeline. - Synonym pairs are both accepted —
heading/header,paragraph/text,quote/blockquote— matching the worker's renderer.
The one hard constraint is the type enum, frozen after auditing the block
types present on stage
(tasks/AISD-205/stage-block-type-audit.md); richtext was added to the
enum because the audit found 114 live rows using it.
How forge shapes map¶
| Forge output | Blocks |
|---|---|
| Native article | heading + paragraph with data.text |
| Legacy listicle (interleaved) | heading + paragraph + image with data.src = "" and data.alt set to the slide heading |
wp_import listicle |
checklist_item with data.{page,title,text,image} |
| Worker image fallback | image with data.url or data.file.url |
tests/admin/test_block_schemas.py keeps a fixture per shape so a schema change
that would break forge fails in cms-api's own suite.
Changing a schema¶
- Add a new numbered hq DDL step under
hq/sql_models/ddl/that updatescontent_types.block_schemafor the affected rows. Never edit an applied step. Keep it idempotent (guard on the current value). - If the change alters a document, update the affected type's builder feeding
SEED_BLOCK_SCHEMASinsrc/cms_api/admin/block_schemas.py, regeneratetests/fixtures/block_schema_seeds.json(the{name: document}mapping), and regenerate that type'sSET @schema_<name> := '...'literal in the DDL — all in lock-step.test_seed_schemas_match_the_fixturealways runs in CI and compares code against the fixture per type;test_seed_schemas_match_the_hq_ddl_seedadditionally compares each type's live DDL literal when a siblinghqcheckout is present locally. - Apply on the target environment (
hq.cms.db bootstrap, thenhq.cms.db check).
No cms-api deploy is needed for a pure data change; running instances pick it up within the 300 s cache TTL.
Validation errors¶
See Content admin API for the 422 content_blocks_invalid
body, which paths validate, why duplicate is exempt, and why
content_blocks: null is a 422 for any seeded (schema-enforced) content type.