# Content model and field types

> Define collections and fields in cms/schema.json, and see how each field type is stored in entry files.

You describe your content in one file, `cms/schema.json`. The editor, the agent skill, the build and the generated
types all read it, and every entry file must match it.

## The schema file

```json
{
  "contentDir": "cms/content",
  "git": { "baseBranch": "main" },
  "collections": {
    "post": {
      "label": "Post",
      "pluralLabel": "Posts",
      "url": "/posts/{slug}",
      "titleField": "title",
      "slugFrom": "title",
      "fields": {
        "title": { "type": "string", "label": "Title", "required": true },
        "body": { "type": "markdown", "label": "Body" }
      }
    },
    "home": {
      "label": "Home page",
      "singleton": true,
      "url": "/",
      "fields": { "title": { "type": "string", "label": "Title" } }
    }
  }
}
```

| Key | Meaning |
|---|---|
| `contentDir` | The entries folder, relative to the project root |
| `git.baseBranch` | The branch the site deploys from; the editor opens pull requests into it |
| `collections.<key>.label` | The name of one entry in the editor (*Post*); a singleton's only name |
| `pluralLabel` | Optional: the name for many entries (*Posts*), for headings; not on a singleton. Without it the editor shows `label` |
| `url` | Optional: the path of the collection's page on your site; see [Page URLs](#page-urls) |
| `singleton` | `true`: the collection has one entry, `index.md` |
| `titleField` | The field that names an entry in lists |
| `slugFrom` | The field the **New** dialog fills with the title; the slug is made from that title |
| `groups` | Optional tabs of the entry in the editor; see [Field groups](#field-groups) |
| `fields` | A map of field key to field |

An unknown key anywhere is an error. Collection and field keys start with a letter and hold only letters and digits.

Optional `git` keys name what the editor and the agent create:

| Key | Default | Placeholders and rules |
|---|---|---|
| `branchPrefix` | `editlet/` | Ends in `/`; must not clash with the base branch |
| `commitMessage` | `{Action} {target}` | `{action}` (`create`, `update`, `delete`, `upload`), `{Action}`, `{collection}`, `{slug}`, `{target}` |
| `pullRequest.title` | `Update {branch}` | `{branch}`, `{name}` (without the prefix), `{login}` |
| `pullRequest.labels` | `["editlet"]` | At most 10; `[]` adds none |
| `pullRequest.draft` | `false` | `true` opens draft pull requests |

## Page URLs

`url` tells the editor where an entry is shown on your site. It is a path from the site's root:

- `/posts/{slug}`: one page per entry. `{slug}` is replaced by the entry's slug (`/posts/tires`), the only placeholder.
- `/`: a fixed path. Every entry of the collection is shown on that one page (sections of the homepage). A singleton's
  `url` is always a fixed path.
- Keep your site's trailing `/` if it has one (`/posts/{slug}/`).
- Leave `url` out for a collection that has no page of its own (authors, site settings).

With `url`, the editor shows an entry's page address and can open the entry's page on the site. Write the slug as
`{slug}`, not as your framework's `[slug]` or `:slug`. A path must start with `/`, use plain ASCII (no spaces, `?`,
`#`, `%`, quotes, `<`, `>` or `//`), have at most 200 characters, and not be under
`/editlet/` or `/__editlet`. Routes that depend on a field (`/2026/05/{slug}`, `/{category}/{slug}`), language
prefixes and base paths cannot be written yet; leave `url` out for those collections.

## Field groups

A collection with many fields can split them into tabs in the editor. List the tabs in `groups` and put `group` on
the fields that belong to a tab other than the first:

```json
"post": {
  "label": "Post",
  "groups": { "content": { "label": "Content" }, "media": { "label": "Media" }, "settings": { "label": "Settings" } },
  "fields": {
    "title": { "type": "string", "label": "Title" },
    "cover": { "type": "image", "label": "Cover image", "group": "media" },
    "featured": { "type": "boolean", "label": "Show in the header", "group": "settings" }
  }
}
```

Fields without `group` are in the first tab. Only top-level fields take `group`, and it must name one of the
collection's `groups`. A tab with changed fields shows an amber dot, one with errors a red dot; with one group the
editor shows no tabs. Groups only change the editor: entry files and types stay the same.

## Entry files

Each entry is one UTF-8 file, `<contentDir>/<collection>/<slug>.md`, with YAML frontmatter and a Markdown body:

```md
---
title: Hello world
cover:
  src: hello-world/cover.webp
  alt: A lighthouse at dusk
draft: true
---

The body is the collection's markdown field.
```

- **Slug:** lowercase letters and digits with single hyphens. The editor makes it from the title and drops other
  characters, so type it yourself for a non-Latin title.
- **Drafts:** `draft: true` keeps the entry out of production builds. New entries from the editor start as drafts,
  copies made with ⋯ → Duplicate too. ⋯ → Set as draft (or Set as published) changes the flag; the editor saves it as any
  edit.
- **Files:** besides entries, `contentDir` may hold only images and names starting with `.`. Anything else is an error.
- **YAML:** the 1.2 core schema, so `2026-01-15` and `yes` stay strings. Saving in the editor drops comments.
- Leave out empty optional fields instead of writing `null` or `''`.

## Field types

There are 14 types. Every field takes `label` (required), `hint` (help text) and `required`; a top-level field can also take `group`.

| Type | Options | In the file |
|---|---|---|
| `string` | `list`, `default` | `title: Hello`; a list of strings with `list: true` |
| `text` | `rows` (editor height), `default` | A string, often a YAML block scalar |
| `markdown` | none | The body. One per collection. Inside an `object` or block: a Markdown string |
| `boolean` | `default`, `labels` (`true` and `false` texts) | `featured: true` |
| `select` | `options` (`{ label, value }`), `multiple`, `default` | `status: live`; a list with `multiple` |
| `number` | `min`, `max`, `integer`, `step` (editor only), `default` | `price: 9.5` |
| `datetime` | `dateOnly`, `defaultNow` | `2026-01-15T09:00:00Z` (`Z`, offset or local time); `2026-01-15` with `dateOnly` |
| `json` | none | Any YAML value |
| `url` | none | `https://…`, `http://…` or a path like `/about` |
| `color` | `default` | `accent: '#1a2b3c'`, quoted because `#` starts a YAML comment |
| `image` | none | `{ src, alt }`, both required |
| `reference` | `collections`, `multiple`, `min`, `max` | `author: author/jane`; a list with `multiple` |
| `object` | `fields`, `list`, `min`, `max` | A mapping of its fields; a list of them with `list: true`. See [Objects and blocks](#objects-and-blocks) |
| `blocks` | `blocks` (kind → `{ label, fields }`), `min`, `max` | A list of mappings, each with `block: <kind>`. See [Objects and blocks](#objects-and-blocks) |

New entries get each `default` (it must pass its own field) and `defaultNow` sets the current time. `min` and `max`
count list items and need `multiple` or `list`. A required list needs at least one item.

## References

A reference is `collection/slug`; a singleton is `<collection>/index`, such as `home/index`. A list cannot hold one reference twice. To build a tree
(a survey with sections with questions), give each level its own collection and a `multiple` reference to the next.

Next.js reads a tree in one call, `getEntry('survey', 'onboarding', { depth: 2 })`; Astro needs one `getEntries` per
level. Cycles are fine: reading stops at `depth`. In the editor, **Rename…** rewrites every reference and image path in
one commit, and **Delete** can remove the references too, unless a required one would break.

## Objects and blocks

Use them for parts that never live on their own. An `object` groups fields (an SEO group), or with `list: true` makes
a list of groups (FAQ items). `blocks` is a list of items of different kinds:

```json
"content": { "type": "blocks", "label": "Content", "blocks": {
  "hero": { "label": "Hero", "fields": { "title": { "type": "string", "label": "Title", "required": true } } },
  "prose": { "label": "Text", "fields": { "text": { "type": "markdown", "label": "Text" } } }
} }
```

```yaml
content:
  - block: hero
    title: Spring sale
  - block: prose
    text: |
      Two weeks of **discounts**.
```

Any field type works inside, nesting included. `block` cannot be a field key in a block, and neither type takes a
`default`. In code, `if (b.block === 'hero')` narrows an item to the hero's fields.

## Images

`src` is relative to the entry file's folder: `cms/content/post/hello-world.md` uses `hello-world/cover.webp`. The
file must exist inside `contentDir` and end in `webp`, `png`, `jpg`, `jpeg`, `avif`, `gif` or `svg`; URLs are errors.
The editor uploads to `<contentDir>/<collection>/<slug>/` in the same commit as the entry. Body images are not checked.

## Validation

The editor checks before each save, the guard hook before a commit, and `next build` or `astro build` fail on an
invalid entry. Run the check yourself:

```bash
node node_modules/editlet/skills/editlet/scripts/validate.mjs
```

It prints `✓ N entries valid`, or one `file: field.path — message` line per issue and exit code 1.

| Message | Fix |
|---|---|
| `Unknown field` | Add the field to the schema, or remove the key |
| `Referenced entry not found: author/jane` | Create the entry, or fix the reference |
| `This collection has no markdown field, but the entry has body content` | Remove the body, or add a `markdown` field |
| `Not an entry file` | Move the file out of `contentDir`, or name it `<collection>/<slug>.md` |

## Generated types

In Next.js, `withEditlet` writes `.editlet/content.d.ts` when `next dev` or `next build` starts; pass its `EntryMap`
to `createReader`. Astro types come from `astro:content`. To write the file by hand, run
`node node_modules/editlet/skills/editlet/scripts/types.mjs`. Do not commit it.

## Change the schema

Ask your agent, for example "rename `cover` to `image` in posts". The skill shows which entries change or lose values
first, then updates the schema and the entries. The editor's **Content model** screen (`?editlet=schema`) shows the
collections, fields and any problems.
