Skip to content
ui8kitPublic

Repository files navigation

@ui8kit/contract

The Codex wire contract for TypeScript, framework-neutral. The current contract is the Codex v0.4 core (draft, agreed in docs/core-draft.md); the v0.3 types and validator stay in the package as legacy.

Codex v0.4 core

One neutral shape for every platform (WordPress, Slim/Flight, GoCMS/GoCRM, Svelte/React/Vue apps, codegen-x fixtures). No platform fields in the contract.

  • Texts (title, slug, content, excerpt, menu labels) are plain strings in one locale. The record key is (id, locale): a translation is another record with the same id and another locale. Short UI strings live in dictionaries ({ locale, source_locale?, messages }).
  • Entry: id, locale, kind, status, visibility, slug, title + optional content, excerpt, author_id, parent_id, featured_media_id, template, terms, order, pinned, format, discussion{comments,pings}, origin[{system,id}], metadata{k:{value,private?}}, published_at, deleted_at, and version, created_at, updated_at.
  • Id rule (draft v0.4): id is ASCII ^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$ (CODEX_ID_PATTERN, isCodexId, assertCodexId); a record from an integer primary key MUST get <kind>-<n> (codexIdForInt); the id never changes and is kept on import, the platform id goes to origin[{system,id}]. canonicalize enforces the pattern; entry.schema.json does not yet (v0.5).
  • Entry status: draft | pending | scheduled | published | archived | trashed. New entities (review, lead, deal, media, user, settings) are a kind with typed fields in metadata, never a status.
  • Settings: { id, locale, title, language, description?, timezone?, logo_media_id?, icon_media_id?, front_page_id?, posts_page_id?, posts_per_page?, comments_open?, currency?, contact?, hours?, hours_exceptions?, map? } + version, created_at, updated_at (validated in settings: [...] of the collection; unique (id, locale)).
  • Settings contact block (all optional, platform-neutral, no nested empty values: omit a key instead):
    • contact: { phone?, email?, address?, links? }: phone E.164 (+12025550143), email a string with UI hint email (schema x-ui-hint, not a type), address one line in the record's locale (localized: each settings record of an id carries its own), links: [{ network, handle }] (messengers and social profiles: telegram, whatsapp, ...; handle without spaces, never a URL; source order kept, never sorted).
    • hours: [{ days: ["mon", ...], open: "HH:MM", close: "HH:MM" }] weekly opening hours in settings.timezone (close up to 24:00; close < open runs past midnight; several rules per day allowed; days not named are closed) and hours_exceptions: [{ date: "YYYY-MM-DD", closed: true } | { date, open, close }] (one per date).
    • map: { provider?, lat?, lng? } (WGS 84, lat and lng together; without coordinates platforms search by contact.address).
    • Phone, email, links, hours, exceptions and map are the same in every locale of one id (warning core.settings.locale_neutral_mismatch).
  • Term: same (id, locale) key, taxonomy_id, name, slug, description?, parent_id?. Comment statuses: approved | hold | spam | trash. Menu: { id, location, locale, items[{ id, label, url, parent_id?, order?, target?{kind,id} }] }.
import { validateCollectionV04, type EntryV04 } from "@ui8kit/contract";

const issues = validateCollectionV04({ entries: [{ file: "post-4.json", data }], terms, comments, menus, dictionaries });

Pass a manifest to check entry metadata against the resource of the entry's kind: validateCollectionV04(collection, { manifest }).

  • Field types are the manifest's own (manifest.schema.json, unchanged): string/text/textarea/richtext/markdown → string, number → finite number (fastygo.codex/integer rule → integer), boolean, datetime → RFC 3339, select → one of options, collection → array (each item checked against items), object → nested fields, relation → id (one-to-one) or id array, which must exist and have the relation's target kind. json, computed, encrypted and unknown types accept any value.

  • required fields missing, empty or null → error manifest.field_required; wrong type → error manifest.field_invalid.

  • Kind not in the manifest → warning manifest.kind_unknown (metadata not checked): new kinds can appear before their manifest. Metadata key not declared for the kind → warning manifest.metadata_unknown. Warnings carry severity: "warning"; errors have no severity.

  • Manifest fields whose id is a core entry key (title, slug, …) describe the core field and are not looked up in metadata.

  • Without a manifest, metadata is not checked at all.

  • Money: a field with the rule fastygo.codex/money holds integer minor units (cents, kopecks: 1250 = 12.50) and needs an ISO 4217 currency: the entry's own metadata field (currency, or the rule arg currency_field; declare it in the manifest) or the default currency of a settings record in the collection, or { currency } in the options. Otherwise error manifest.currency_missing (one per entry). An entry has one currency: money inside nested collection / object fields (order lines) uses it too. Amounts must be safe integers (|n| ≤ 2^53 − 1, so JS, Go and PHP read them exactly); zero and negative amounts (refunds, discounts) are valid; floats and numeric strings are not.

  • Metadata keys equal to a core entry field (status, excerpt, kind, …) give the warning core.metadata_key_shadowed, with or without a manifest.

The WordPress adapter exports wpManifest() (post/page with the footnotes meta seen on the sample site; pass extraFields for more registered meta).

The validator checks each record's shape (strict keys) and the set rules: (id, locale) unique; entry slug unique within (kind, locale, parent_id); term slug unique within (taxonomy_id, locale, parent_id); parent / term / comment references.

Schemas (draft 2020-12), stable unversioned URLs (no consumers yet; versioned URLs start at v0.5):

Schema URL
entry https://schema.buildy.tw/entry.schema.json
term https://schema.buildy.tw/term.schema.json
taxonomy https://schema.buildy.tw/taxonomy.schema.json
comment https://schema.buildy.tw/comment.schema.json
menu https://schema.buildy.tw/menu.schema.json
dictionary https://schema.buildy.tw/dictionary.schema.json
settings https://schema.buildy.tw/settings.schema.json
manifest https://schema.buildy.tw/manifest.schema.json (shape unchanged from v0.3)
revision https://schema.buildy.tw/revision.schema.json

The old /v0.3/… and /latest/… paths are removed.

View profile view/v1

schemas/view.schema.json (https://schema.buildy.tw/view.schema.json, source src/view-v1.ts + scripts/schemas-view-v1.ts) describes the context a template or static site generator receives for one rendered page, for any Codex v0.4 backend.

  • The view context is a projection of Codex records: core record fields are $ref'd from the v0.4 schemas (entry, term, menu, settings, dictionary, comment), never copied.
  • On top of that there is a closed set of render-time primitives: link, image, trustedHtml, pagination, menuTree (resolved tree with current/ancestor), head (seo), shell (site from settings, labels from dictionary, request/page state).
  • Root defs: pageContext (front, single, page, attachment, any kind), archiveContext (home, category, tag, taxonomy, kind, author, date, search), errorContext (404).
  • Arbitrary entry fields go in fields, each typed by its Manifest field type; the mapping is FIELD_TYPE_VIEW_V1.
  • The profile has its own version (x-view-profile: view/v1, x-view-version). Minor versions only add things.
  • fixtures/view/*.json holds one example per page type; test/view-v1.test.ts validates them.

Page spec page-spec/v2 and page patch page-patch/v2

schemas/page-spec-v2.schema.json (https://schema.buildy.tw/page-spec-v2.schema.json) and schemas/page-patch-v2.schema.json (https://schema.buildy.tw/page-patch-v2.schema.json), source src/page-spec-v2.ts + scripts/schemas-page-spec-v2.ts. A page is one document, equal to what a host exports: { version: 2, title, slug, locale, seo, blocks: [{ id, item, variant, props }] }, where item is the registry item name of npx ui8kit add, variant one of its published variants and props exactly the props of its component (the host validates them against registry.buildy.tw/props/v0.json). There is no kind, copy slot, children list or screen family in the document. Edits are RFC 6902 JSON Patches (page-patch/v2) or a full document; a failure is reported as compiler-style { path, expected, got }. page-spec/v1 below stays published unchanged, side by side. Fixtures: fixtures/page-spec-v2/, fixtures/page-patch-v2/; test/page-spec-v2.test.ts.

Page spec page-spec/v1 and page patch page-patch/v1

schemas/page-spec.schema.json (https://schema.buildy.tw/page-spec.schema.json) and schemas/page-patch.schema.json (https://schema.buildy.tw/page-patch.schema.json), source src/page-spec-v1.ts + scripts/schemas-page-spec-v1.ts, describe the page document a BuildY agent proposes and a host saves. It is not a Codex v0.4 entry.

  • The page spec is the primary artifact: history, edits and rollback work on this JSON; code for a UI8Kit target (Svelte today) is always regenerated from it.
  • Root: the page, identical to the buildytech/chat Page DTO (layout.version 1, parsePage), so every saved page validates unchanged. $defs/provenance, record and store describe the browser page store (buildy:v1:pages, migrated once from the legacy key buildytech-creator-pages-v1), including legacy records without provenance.
  • After the schema, consumers run the program checks of x-program-checks (PAGE_PROGRAM_CHECKS_V1): duplicate ids, block count, payload size. JSON Schema maxLength counts code points; parsePage counts UTF-16 units.
  • Slots: x-page-slots (PAGE_SLOTS_V1, PAGE_ITEM_SLOTS_V1) lists the copy keys each kind renders and the row kind, row limit and row fields of list kinds. A saved page may hold other copy keys; a patch may only write slots.
  • Catalog source (1.1.0, optional): catalogBlock names the Stock catalog block asset a block was built from (block.<name>) and variant one of its catalog variants (block.<name>/<variant> is the catalog asset id). A host renders the registry organism of that block with the variant's props and rejects blocks, variants and icon names its pinned catalog / registry snapshot does not have. Copy keys icon (Lucide name) and href (link of action) feed the organism props.
  • Props overrides (1.3.0, optional, top-level blocks only): props maps JSON Pointers of the props of the registry item the block's catalog variant renders (registry.buildy.tw/props/v0.json, e.g. /actions/secondary, /content/card/title, /content/features) to plain JSON values (strings are safe text). The host merges them over the variant preset and the page content (copy slots keep carrying the text they map) and validates the merged props against the item's props schema: x-host-checks (PAGE_HOST_CHECKS_V1: props_invalid). Pointers do not overlap. A consumer without the registry schema keeps the map unchanged.
  • Screen family (1.4.0, optional, provenance only): family (dashboard, landing, food-grill, restaurant, generic) and familyPinned. Advisory: the host derives it from the page composition after every operation (the role majority; mixed -> generic) unless pinned, and uses it to rank blocks and suggest a preset; it never rejects a block (hard rules stay: header first and footer last when present, a renderer exists, the props schema passes).
  • page-patch/v1 is an ordered list of operations (set, set_items, add, replace, remove, move, set_page, set_variant), applied in order, all or nothing. add and replace name a kind (and optionally a variant); the host builds the block from the page's Stock screen family and sets catalogBlock itself. replace (1.2.0) and add (1.5.0) may name catalogBlock (block.<name>, a block of that kind): with it on add the page may hold several blocks of one kind (block.steps, then block.pricing).
  • fixtures/page-spec/ holds real pages assembled from the pinned Stock catalog (dashboard, landing, website), the chat contract fixture, a store blob and the parsePage conformance cases; fixtures/page-patch/ holds patches. test/page-spec-v1.test.ts validates them and checks that the schema and parsePage agree on every conformance case.

Session buildy:v1

schemas/session.schema.json (https://schema.buildy.tw/session.schema.json), source src/session-v1.ts + scripts/schemas-session-v1.ts, describes a portable BuildY session file: what a host exports (window.buildy.export() in buildytech/chat), what any BuildY host imports and what Lab opens for review. It holds no credential.

  • Root: format (buildy:v1), exportedAt, app (name, version, host, optional commit), pins (Stock contentCommit, registry sourceCommit, optional catalog digest), sessions, optional selectedSessionId and non-secret settings.
  • A session is one chat: messages (user / assistant, Ask or Build mode, proposal, run), page (a page-spec/v1 record by $ref), pageRevision (equals page.revision) and history (one entry per committed change: apply, edit, undo, redo; typing in one field is one edit; an unchanged value adds none).
  • skin (1.1.0, optional): the skin of the chat's page, the id of the registry item theme-<skin> (npx ui8kit add theme-<skin>); null or absent is the default theme.
  • prefs (1.2.0, optional): { layout }, the user's shell layout as the host keeps it in buildy:v1:prefs: navWidth, chatWidth, inspectorWidth, layersHeight, treeWidth (integer px, 0..4096, clamped by the host) and navCollapsed, chatCollapsed, inspectorCollapsed, layersCollapsed, treeCollapsed, focus.
  • collection (1.3.0, optional, per chat): the Stock collection (buildy-collection:v1) the chat was opened from: { id, title, importedAt?, assetsDigest?, items, missing? }, items by $ref to collection.schema.json#/$defs/item; items were found in the host snapshot and bound Build, missing were skipped (docs/collection-intake.md).
  • plan (1.4.0, optional, per message): an Ask page plan, { status, pageId, baseRevision, summary?, ops, executedAt? }; each op is a page-patch/v1 op (set_variant, set, set_items, add, replace, remove, move) on a block of the chat's page with the host's check (valid, issue { code, message, allowed? }) and, after Apply in Build ran it without a model call, its result (applied, rejected, skipped). A proposal made that way has origin: "ask-plan". 1.5.0: a replace op may name catalogBlock (another Stock catalog block of the same kind, page-patch/v1 1.2.0) and dropped (text fields the new block does not render).
  • history[] entries (1.6.0): optional op (set_field, set_variant, add, move, remove, insert, replace) and path (JSON Pointer of the field or row in the block's render props, e.g. /actions/cta); never the value.
  • history[] op (1.7.0): set_family, the page's screen family changed (derived or pinned), its own step.
  • Objects are closed except the host records in x-open-records (usage, run usage, proposal catalog refs). After the schema, importers run x-program-checks (SESSION_PROGRAM_CHECKS_V1): duplicate session ids, page revision mismatch, non-monotonic history, unknown selected session.
  • An importer with other pins still opens the file and re-validates every page against its own catalog.
  • fixtures/session/ holds a real file exported from the chat and a minimal file; test/session-v1.test.ts validates them and rejects broken files.

Session buildy:v2

schemas/session-v2.schema.json (https://schema.buildy.tw/session-v2.schema.json), source src/session-v2.ts + scripts/schemas-session-v2.ts, is the session file of the simple line (0.5.0). It holds no credential.

  • Root: format (buildy:v2), exportedAt, app, pins (registry { url, sourceCommit }, prototypes { repo, commit }, optional catalogDigest), sessions, optional selectedSessionId, settings and prefs (layout without the old chatWidth / chatCollapsed).
  • A session is a project: page (a page-spec/v2 record or null), pageRevision, skin, history (op without set_family) and ext; the chat is ext.chat (mode, selectedModelId, usage, messages). No flat chat keys, no scenarioId / screenId, no collection.
  • Messages carry no artifacts and no plan; a proposal (Build only) is { applied, status, source, liveModel, warnings, missing, title, locale, baseRevision, diff }, the page itself is in the record.
  • Same x-program-checks and x-limits as buildy:v1; open records: ext.chat.usage, run.usage, run.rejected.
  • fixtures/session-v2/ (studio, minimal); test/session-v2.test.ts also rejects every buildy:v1 legacy key.

Collection buildy-collection:v1

schemas/collection.schema.json (https://schema.buildy.tw/collection.schema.json), source src/collection-v1.ts + scripts/schemas-collection-v1.ts, describes a set of Stock screen pages and block variants a person gathers on Stock (app.codegen-x.com, Collections) and hands to BuildY (Open in BuildY, export / import). It holds no credential and no personal data.

  • Root: format (buildy-collection:v1), id, title, createdAt, optional updatedAt, source (the app), pins (Stock repo + assetsDigest, optional contentCommit; optional registry), optional skin, items (at most 100, picked order).
  • An item is a screen page (kind: "screen", id = screen.<screen>/<area>) or a block (kind: "block", id = block.<name>, optional variant, optional registry name), each with an optional title and note. Ids are Stock asset ids, the entity keys of the BuildY catalog snapshot; a block with a variant is the asset block.<name>/<variant> (page-spec/v1 catalogBlock + variant).
  • After the schema, readers run x-program-checks (COLLECTION_PROGRAM_CHECKS_V1: duplicate_item, empty_collection). splitCollectionV1 splits the items into the ones a host catalog has and the missing ones; missing items are reported, never substituted.
  • The schema stays inside the keyword subset of the chat's browser validator (a test guards it).
  • How the chat takes a collection: docs/collection-intake.md. fixtures/collection/ holds a real collection and a minimal one; test/collection-v1.test.ts validates them and rejects broken files.

Block contract block-contract/v1

schemas/block-contract.schema.json (https://schema.buildy.tw/block-contract.schema.json), source src/block-contract-v1.ts + scripts/schemas-block-contract-v1.ts, describes the contract of one block of the simple line that codegen-x packages/prototypes generates and the registry publishes (https://registry.buildy.tw/contracts/<item>/<version>.json): what a builder needs to drive the block without reading its code.

  • Root: item, version, block, organism, line, model + modelSchema, axes / defaults, variants, defaultVariant, settings ({ type, default? } per setting), modes / defaultMode, places ({ place, kind, fields, list? }, fields are JSON Pointers, * an index of list), wireframe.items. Every object is closed.
  • variants[]: { id, label, axes, settings? }. The optional settings are the variant's own setting defaults over settings[*].default: align (start | center | end, logical), padding (compact | standard | spacious), level (1–3). A centred composition (HeroSimple mediaTop) starts with align: "center". A saved block (page-spec/v2 { id, item, variant, props }) overrides them in props; the variants stay layout-only. variantSettingsV1(contract, variant) gives the start values.
  • After the schema, readers run x-program-checks (BLOCK_CONTRACT_PROGRAM_CHECKS_V1: default_variant_unknown, variant_axis_unknown, variant_axis_value, default_mode_unknown; blockContractProgramIssuesV1).
  • fixtures/block-contract/ holds the published hero-simple and team-simple contracts; test/block-contract-v1.test.ts validates them and rejects broken ones (a physical align, an unknown setting).

Issue codes

validateCollectionV04 returns IssueV04[] = { code, path, message, severity? }. Errors have no severity; warnings carry severity: "warning" and never make a record invalid. The registry is ISSUE_CODES_V04 (code → meaning) with WARNING_CODES_V04; a test proves every emitted code is registered and listed here. Match on code, never on message (messages may be reworded in any release).

Grammar: <layer>.<scope>.<problem>, or <layer>.<field>_<problem> for fields every record type shares. Layers: decode (not an object / unknown field), core (rules of the neutral core), manifest (metadata against a manifest, only when one is passed). Problems: _required (absent or blank), _invalid (type, enum, format), _duplicated (uniqueness), _missing (reference to a record not in the collection), _unknown (not declared), _cycle, _self, _order, _shadowed, _incomplete.

Code Severity Meaning
decode.invalid error the value is not an object of the expected record type
decode.unknown_field error a field the record type does not have (records are closed)
core.id_required error id is absent or blank
core.locale_invalid error locale is not a BCP 47 tag
core.version_required error version is not an integer >= 1
core.timestamps_required error created_at / updated_at are not RFC 3339
core.timestamps_order error updated_at precedes created_at
core.origin_invalid error origin is not an array of {system, id}
core.metadata_invalid error metadata is not an object of {value, private?}
core.metadata_key_required error a metadata key is blank
core.metadata_key_shadowed warning a metadata key equals a core entry field (status, excerpt, ...)
core.entry.type_invalid error an optional entry field has the wrong type
core.entry.kind_invalid error kind is not an identifier (^[a-z][a-z0-9_-]{0,62}$)
core.entry.status_invalid error status is not one of ENTRY_STATUSES_V04
core.entry.visibility_invalid error visibility is not public | private
core.entry.slug_required error slug is blank
core.entry.title_required error title is not a string
core.entry.time_invalid error published_at / deleted_at is not RFC 3339
core.entry.published_at_required error status scheduled without published_at
core.entry.deleted_at_required error status trashed without deleted_at
core.entry.terms_invalid error terms is not an array of {taxonomy, term_id}
core.entry.key_duplicated error (id, locale) is not unique among entries
core.entry.slug_duplicated error slug is not unique within (kind, locale, parent_id)
core.entry.parent_missing error parent_id names no entry of the collection
core.entry.parent_self error parent_id equals the entry's own id
core.entry.parent_kind_invalid error the parent entry has another kind
core.entry.parent_cycle error the parent chain loops back to the entry (per locale)
core.term_ref.incomplete error a term reference lacks taxonomy or term_id
core.term_ref.duplicated error the same term is referenced twice by one entry
core.term_ref.missing error a term reference names no term of the collection
core.term.type_invalid error an optional term field has the wrong type
core.term.taxonomy_invalid error taxonomy_id is not an identifier
core.term.name_required error name is blank
core.term.slug_required error slug is blank
core.term.key_duplicated error (id, locale) is not unique among terms
core.term.slug_duplicated error slug is not unique within (taxonomy_id, locale, parent_id)
core.term.parent_missing error parent_id names no term of the taxonomy
core.term.parent_self error parent_id equals the term's own id
core.term.parent_cycle error the parent chain loops back to the term (per taxonomy and locale)
core.comment.entry_required error entry_id is blank
core.comment.content_required error content is not a string
core.comment.status_invalid error status is not one of COMMENT_STATUSES_V04
core.comment.key_duplicated error (id, locale) is not unique among comments
core.comment.entry_missing error entry_id names no entry of the collection
core.menu.location_required error location is blank
core.menu.items_required error items is not an array
core.menu.item_incomplete error a menu item lacks id, label or url
core.menu.item_duplicated error two items of one menu share an id
core.menu.parent_missing error an item's parent_id names no item of the menu
core.menu.parent_cycle error a menu item is its own ancestor
core.menu.target_invalid error target is not {kind, id}
core.menu.key_duplicated error (id, locale) is not unique among menus
core.dictionary.messages_required error messages is not an object
core.dictionary.value_invalid error a message is not a string
core.settings.type_invalid error an optional settings field has the wrong type
core.settings.title_required error title is blank
core.settings.language_invalid error language is not a BCP 47 tag
core.settings.timezone_invalid error timezone is not an IANA zone name
core.settings.currency_invalid error currency is not ISO 4217 (three capital letters)
core.settings.key_duplicated error (id, locale) is not unique among settings
core.settings.phone_invalid error contact.phone is not E.164 (+ and 2 to 15 digits, no spaces)
core.settings.email_invalid error contact.email does not look like an email address
core.settings.link_invalid error a contact.links item is not {network, handle} (lowercase identifier, handle without spaces)
core.settings.hours_invalid error an hours rule is not {days, open, close} (unique weekdays mon..sun, HH:MM, close not equal to open)
core.settings.hours_exception_invalid error an hours exception is not {date, closed: true} or {date, open, close} with an existing date
core.settings.hours_exception_duplicated error two hours exceptions name the same date
core.settings.map_invalid error map is not {provider?, lat?, lng?} (identifier provider; lat -90..90 and lng -180..180, together)
core.settings.locale_neutral_mismatch warning phone, email, links, hours, exceptions or map differ between locales of one settings id (only contact.address is localized)
manifest.invalid error the manifest is not {name, version, resources[]}
manifest.kind_unknown warning no resource of the manifest describes the entry's kind
manifest.metadata_unknown warning a metadata key the resource does not declare
manifest.field_required error a required manifest field is absent from metadata
manifest.field_invalid error a metadata value does not match its manifest field type or rule (integer and money: safe integers)
manifest.currency_missing error a money field without a currency (entry, settings or options)
manifest.currency_invalid error the currency of a money field is not ISO 4217

Extension points

The core is closed: records reject unknown fields, and the core never grows platform fields. It is extended in four places, none of which touch the core:

  1. New entity = new kind. A review, lead, deal, product or media item is an EntryV04 with its own kind and typed metadata. Describe it as a manifest resource (record.id = kind, fields, relations) and pass { manifest } to the validator. An undeclared kind is only a warning, so data can arrive before its manifest.
  2. Field rules. fastygo.codex/integer, fastygo.codex/uri, fastygo.codex/money (args: currency_field). Unknown rule names are ignored by this validator, so other tools may add their own.
  3. Adapters live outside the core. An adapter maps a platform record to core records plus a sidecar with whatever the core does not carry (passwords, local dates, original key sets), so platform → core → platform is lossless. The WordPress adapter (@ui8kit/contract/adapters/wordpress) is the reference: it is not exported from the package index, and the core never imports it. Write new adapters the same way (fromX(record) → { entry, sidecar }, toX(entry, sidecar) → record) and test them with a real-data round trip.
  4. Translations are records with the same id and another locale; short UI strings go to dictionaries. No localized objects.

Public API and semver

Stable surface of 0.2 (changes are listed in CHANGELOG.md):

  • @ui8kit/contract: v0.4 types (EntryV04, TermV04, TaxonomyDefinitionV04, CommentV04, MenuV04, MenuItemV04, DictionaryV04, RevisionV04, SettingsV04, ContactV04, ContactLinkV04, HoursRuleV04, HoursExceptionV04, MapV04), constants (CODEX_V04_VERSION, ENTRY_STATUSES_V04, WEEKDAYS_V04, LOCALE_NEUTRAL_SETTINGS_V04, COMMENT_STATUSES_V04, VISIBILITIES_V04, TAXONOMY_MODES_V04, REQUIRED_V04, KEYS_V04, ISSUE_CODES_V04, WARNING_CODES_V04, schemaUrlV04), the validator (validateCollectionV04, check*V04, the contact patterns E164_PATTERN, EMAIL_PATTERN, HANDLE_PATTERN, TIME_PATTERN, CLOSE_PATTERN, DATE_PATTERN) and its types (IssueV04, IssueCodeV04, ValidateV04Options, CollectionV04, RecordFile).
  • @ui8kit/contract/adapters/wordpress: fromWpPost/toWpPost, fromWpTerm/toWpTerm, fromWpSettings/toWpSettings, WP_CONTACT_OPTIONS, fromWpMedia, fromWpUser, stripWpComputed, WP_COMPUTED, wpManifest and their types.
  • JSON Schemas at https://schema.buildy.tw/<name>.schema.json and schemas/*.schema.json in the package.
  • Issue code values (not messages).

Single source: src/v04.ts holds the types, enums and required lists; scripts/schemas-v04.ts builds the JSON Schemas from them; src/validate-v04.ts holds the strict key lists. test/agreement.test.ts proves at compile time that the types have exactly KEYS_V04 / REQUIRED_V04, and at run time that the schemas have the same keys, required fields and enums, and that ajv and the validator give the same verdict on the minimal record, on each missing required field and on unknown fields.

Policy:

  • The package is 0.x until Codex v0.5. While on 0.x, a minor release (0.2 → 0.3) may change the public surface, rename issue codes or tighten a schema; every such change is listed under "Breaking" in CHANGELOG.md. A patch release only fixes bugs and adds tests, docs or new optional exports.
  • Schema URLs are unversioned and are overwritten in place until v0.5 (there are no outside consumers yet). From v0.5 schemas are published at versioned paths, and the package moves to 1.0 with normal semver: breaking changes only in a major.
  • Everything not listed above (scripts/, test helpers, the legacy v0.3 API, docs/) is internal.

WordPress adapter (outside the core)

import { fromWpPost, toWpPost, fromWpTerm, toWpTerm } from "@ui8kit/contract/adapters/wordpress";

const { entry, sidecar } = fromWpPost(wpPost, { locale: "en" }); // REST v2 post/page, context=edit
const back = toWpPost(entry, sidecar);                            // equal to wpPost minus computed fields
  • Not exported from the package index. Mapping: type→kind, status (publish→published, future→scheduled, pending→pending, private→published + visibility:private, trash→trashed), *.raw→title/content/excerpt, sticky→pinned, menu_order→order, comment_status/ping_status→discussion, categories/tags→terms (category, post_tag), meta→metadata (keys with _ are private), WP id → origin[{system:"wordpress"}]. Default core ids are ${kind}-${wpId} (post-4, user-1, media-20, category-2); override with idFor/wpId.
  • All taxonomies of a type become terms (categories→category, tags→post_tag, project_type, product_cat, …): by default every top-level array of integers is a taxonomy named after its REST field; pass taxonomies: { restField: taxonomy } to set them explicitly (from /wp/v2/types/<type>).
  • metaPrefix strips a meta prefix (_shop_price → price); typed REST meta stays typed; meta that is null in REST (not set) is left out of the core record.
  • The sidecar keeps WordPress-only values outside the contract: password, local date/modified, whether date_gmt was null, the original key set (so toWpPost writes back only keys the object had: a project gets no author/comment_status), the taxonomy fields, meta key names and null meta.
  • fromWpMedia(m, opts) / fromWpUser(u, opts & { at?, personal? }) (WP → core only): /wp/v2/media → kind media (media-<n>; source_url→file, mime_type→mime, media_details.filesize/width/height→size/width/height, alt_text→alt, caption.raw→caption, attached post → attached_to (<kind>-<n> via resolveKind / parentKind); description.raw→content; inherit→published), /wp/v2/users → kind user (user-<n>; name→title, description→content, url in metadata; username→login and roles only as private:true; email/first_name/last_name/nickname only with personal: true and private:true; never password, capabilities, avatar_urls, WP meta persisted_preferences). Export media/users from context=edit: context=view gives an incomplete record (media without title, no content/caption, users without login/roles/dates); the adapter does not guess from rendered HTML. All adapters throw on an id that breaks the id rule (also from a custom idFor). Fixtures fixtures/wordpress/{media,users}.{edit,view}.json are live /wp/v2 REST bodies (local WordPress 7.1.3 demo, host rewritten to http://localhost:8081, test accounts on example.com).
  • fromWpSettings / toWpSettings: /wp/v2/settings ↔ core settings (language en_US ↔ en-US, site_logo/site_icon → media ids, show_on_front+page_on_front → front_page_id, default_comment_status → comments_open, wpfasty_currency or { currency } → currency, the wpfasty-types options WP_CONTACT_OPTIONS (wpfasty_contact_phone, wpfasty_contact_email, wpfasty_contact_address, wpfasty_contact_links, wpfasty_hours, wpfasty_hours_exceptions, wpfasty_map) → contact.*, hours, hours_exceptions, map; unset options stay in the sidecar). URL, admin email, date formats etc. stay in the sidecar.
  • Dropped computed fields: link, guid, *.rendered, *.protected, content.block_version, class_list, generated_slug, permalink_template, term count, _links, _embedded.
  • Not covered by WP REST: the site locale (pass locale), term timestamps (pass at), trash time (deleted_at = modified_gmt).
  • fixtures/wordpress/*.json are real context=edit responses from local WordPress sites (hosts replaced with http://localhost…): posts, pages, categories, tags, settings (latte-press), projects + project_type (latte-folio), products + product_cat with typed meta in cents (latte-shop, wpfasty-types plugin). The tests run WP → v0.4 → WP on each of them and validate the converted records with ajv and the validator.

Legacy v0.3

The v0.3 API is still exported:

  • types for Entry, Manifest (Resource, RecordType, Field, Relation), taxonomy (TaxonomyDefinition, Term, TaxonomyAssignment) and Revision, with the wire (snake_case) field names;
  • JSON Schemas (draft 2020-12) for entry, manifest, taxonomy and revision;
  • a strict validator, a TypeScript port of the Codex and fastygo/modules/content-json checks, with issue codes that follow the Go names;
  • a snapshot loader (Library), readers (pick, localized, meta, termIds) and the adapter pattern (defineAdapter).

The package has no runtime dependencies. It ships no app-specific adapters.

Install

npm i @ui8kit/contract     # or: bun add @ui8kit/contract

Usage

import { loadOrError, defineAdapter, pick, meta, SnapshotError } from "@ui8kit/contract";

const lib = loadOrError(manifestJson, files /* [{ file, data }] */);
if (lib instanceof SnapshotError) throw lib; // lib.issues: [{ code, path, message }]

const toCard = defineAdapter("page", (e, { locale }) => ({ id: e.id, title: pick(e.title, locale) }));
const cards = lib.byKind("page").map((e) => toCard(e, { locale: "en" }));
import { validateSnapshot, validateTaxonomy, validateRevision } from "@ui8kit/contract/validate";

validateSnapshot(manifest, files);                  // Go-loader parity
validateSnapshot(manifest, files, { extra: true }); // + extra.* checks (stricter than Go)

Schemas are importable or fetchable:

import entrySchema from "@ui8kit/contract/schemas/entry.schema.json" with { type: "json" };
{ "$schema": "https://schema.buildy.tw/entry.schema.json" }

Schemas and the validator

The JSON Schemas check structure only: keys, types, enums, identifier patterns and RFC 3339 timestamps. They reject unknown keys, as the Go strict decoder does. The semantic rules are in validateSnapshot, not in the schemas:

  • field placement (a localized field goes to locales[*].data, any other field to metadata);
  • select options and fastygo.codex/* rules;
  • relations, parents, taxonomy assignment, duplicate ids;
  • timestamp order.

A document can pass ajv and still fail the validator. Use both.

extra.* checks

{ extra: true } turns on two checks that the Go loader does not run:

  • author_id must point to a user entry;
  • each terms[] item must point to a term entry of the taxonomy it names.

They are off by default.

Versioning

  • The package uses its own semver (see "Public API and semver" above).
  • CODEX_VERSION / package.json#codex = 0.4.0. v0.3 JSON Schemas are no longer published (in-memory legacy copies in scripts/schemas.ts keep testing the v0.3 fixtures).
  • The static site is public/ (Vercel, vercel.json sets CORS and application/schema+json on *.schema.json). From v0.5 schemas get versioned paths.

Develop

bun install
bun run gen        # writes schemas/ and public/ from scripts/schemas-v04.ts
bun run check      # typecheck + tests + broken-snapshot self-test + build

The tests check three things:

  • the generated files are up to date;
  • the schema properties match the validator's strict key lists, which mirror the Go struct tags;
  • the Codex conformance fixtures and the snapshot suite pass both ajv and the TS validator.

Sources and gaps

  • Types and fixtures come from fastygo/codex v0.3.0 (content, schema, taxonomy, revision, conformance/fixtures). Codex publishes no JSON Schema; these schemas are derived here.
  • The validator is ported from ui8kit/spline-svelte (packages/content). In that repo, 15 broken snapshots were checked against fastygo/modules/content-json v0.1.0. The Go comparison has not been re-run in this repo.
  • Not ported:
    • the manifest digest codex-manifest/v1:sha256:… (Go json.Marshal bytes, kept in Go for compatibility). Ported as a draft instead: entry canonicalization canonicalize / digest (profile codex-entry/v1, vectors fixtures/vectors/canonical.json) and the manifest digest canonicalizeManifest / manifestDigest (profile codex-manifest/v2 = SHA-256 of JCS of the full manifest under the same rules, array order kept; the short {name, version, resources[{id, collection}]} form is not normative; vectors fixtures/vectors/manifest.json), and term / settings digests canonicalizeTerm / termDigest (profile codex-term/v1, computed count dropped) and canonicalizeSettings / settingsDigest (profile codex-settings/v1) under the entry rules (vectors fixtures/vectors/term.json, settings.json), all in src/canonical.ts, see docs/canonical.md;
    • Resource.PublicProjection;
    • fastygo.codex/* rules other than integer and uri;
    • the full Codex locale grammar (here a locale only has to be lower-case and trimmed).
  • taxonomy.Assignment has a type and a $defs entry but no validator.
  • Integers above 2^53 (e.g. source_sequence in the fixture) lose precision in JSON.parse (the validator); canonicalize reads the raw text and rejects them with canonical.unsafe_integer.
  • fixtures/snapshot is the CMS demo snapshot from spline-svelte. fixtures/codex is copied from Codex (MIT, see fixtures/codex/LICENSE).

License

MIT

Releases

Packages

Contributors

Languages