Fields

Pick a field type, set its options and check its values.

Each key in fields is one field. Its type says what it stores and which input the admin shows:

cms/collections/posts.ts
export const posts = defineCollection({
  fields: {
    title: { type: "text", required: true },
    slug: { type: "slug", from: "title", unique: true },
    category: { type: "select", options: ["news", "guide"] },
    publishedAt: { type: "date" },
  },
});

Field types

TypeStores
arrayA list of items with the same fields
blocksA list of page sections of different types
booleantrue or false
date, datetimeA calendar day, or a moment in UTC
fileAny file from the media library
groupFields nested under one key
imageAn image from the media library
linkA link to a document, a URL or a file
numberA number
relation, joinIds of other documents, and the reverse side
richtextA structured document with formatting
selectOne or more of the listed options
sizeA number with a CSS unit, { value, unit }
slugA URL part, like about-us
text, textareaA string
custom:<name>Any value, with your own input in the admin

Common options

Every field type takes these options. The page of each type lists its own options.

label
string | { en, de, … }
The label in the admin. Defaults to the key. Pass an object to translate it.
description
string | { en, de, … }
Help text below the input.
required
boolean
The value MUST be present. A list needs at least one item.
default
value | ({ user, … }) => value
The value of a new document, when the field is missing. A plain value also fills the new form in the admin. A function, like ({ user }) => user?.id, runs on the server only.
unique
boolean
No other document of the collection has the same value. See Validation.
validate
(value, args) => true | string
Your own check. See Your own checks.
condition
Condition
Show the field only when other values match. See Conditions.
private
boolean
Public reads leave the value out. Signed-in users and server code see it.
access
{ read, create, update }
Who can read and write this field. See Access control.
localized
false
In a localized collection, every field is stored per language. false shares this field between languages. Only on top-level fields. See Localization.
hooks
FieldHooks
Functions that run for this field on every write and read, before the collection's hooks. See Hooks.

Limits

  • default applies only when a document is created, and only to fields that are missing.
  • required, default, hidden and private do not apply to join, because a join stores nothing.

Hide or protect a field

hidden and readOnly change only the admin (see Admin options). They are not access rules.

GoalUse
Keep a value out of the admin formhidden
Show a value, but block edits in adminreadOnly
Keep a value out of public readsprivate
Decide who can read or write a valueaccess

Admin options

These options change how the admin form shows a field. The stored document keeps its shape, and the API ignores them.

hidden
boolean
Admin only
The admin does not show the input. The value still saves. Use it for values a hook sets.
readOnly
boolean
Admin only
The admin shows the input disabled. Code and hooks can still write it.
width
"1/4" | "1/3" | "1/2" | "2/3" | "3/4" | "full"
Admin only
The share of the row the field takes. Fields next to each other fill a row. In a narrow form, every field takes the full width. Default "full".
separator
true | string
Admin only
A line above the field, to break up a long form. A string adds a heading above the line.
tab
string | string[]
Admin only
The tab that shows this field. Define the tabs with tabs on the collection, global, group or list item.
fieldset
string
Admin only
The box that holds this field. Define the boxes with fieldsets.
fields: {
  firstName: { type: "text", width: "1/2" },
  lastName: { type: "text", width: "1/2" },
  bio: { type: "textarea", separator: "About" },
  syncedAt: { type: "datetime", readOnly: true },
}

tabs and fieldsets are explained in Collections and globals.

Reading values in TypeScript

The field type sets the TypeScript type of a read. Collections and globals shows how reads are typed:

TypeTypeScript type
text, textarea, slugstring
date, datetimestring, as ISO text
numbernumber
booleanboolean
selectThe union of the options, or an array of it with many
relation, image, fileAn id string, or string[] with many
size{ value, unit }, with the units as a union
groupAn object of its fields
arrayA list of objects of its fields, each with an id
blocksA list of objects, told apart by blockType
richtext, linkThe shapes of the rich text and link pages
const post = await cms.findOne("posts", { where: { slug } });
post?.title; // string, because the field is required
post?.category; // "news" | "guide" | null | undefined
  • required removes null and undefined from the type. Without it, a field can also be null or missing.
  • populate replaces ids with documents, and select drops fields. See Reading.

Validation

Tenon checks every write on the server. The admin runs most checks too, so editors see errors before they save.

Built-in checks

These options check a value without any code. An empty optional value skips them. Add required to demand a value.

The value MUST be present. A list needs at least one item.The fewest characters.The most characters.A regular expression that the whole value MUST match. Write it as a string, so escape each backslash: "\\d{5}".The smallest value.The largest value.The value MUST be min (or 0) plus a whole multiple of step, like the HTML step attribute.The fewest items.The most items.No other document has the same value. Only the server checks it. See the rules below.
zip: { type: "text", pattern: "\\d{5}" },
rating: { type: "number", min: 1, max: 5 },
email: { type: "text", unique: true },

unique works on top-level text, textarea, number, date, datetime, select, slug and single relation fields of collections:

  • It does not work in groups, lists or globals.
  • The comparison is exact and case-sensitive.
  • Empty values never collide.

Your own checks

validate is a function that returns true or an error message:

end: {
  type: "date",
  validate: (value, { siblingData }) =>
    !value || value >= siblingData.start || "Ends before start",
},

The function gets the value and an object with these keys:

KeyHolds
siblingDataThe values next to this field: same document, group or list item
dataThe whole document
originalDocThe stored document. Empty on create
operationThe write: create, update or setGlobal
cmsThe local API. Use it for lookups
userThe signed-in user

The function can be async. It runs on the server only.

Standard Schema

validate also takes a Standard Schema. Zod, Valibot and ArkType schemas all work, so you can reuse a schema you already have:

import { z } from "zod";

email: { type: "text", validate: z.email("Not an email") },
website: { type: "text", validate: z.url() },
  • The first issue of the schema is the error message.
  • An empty value (null, missing or "") skips the schema, like the built-in checks. Add required to demand a value.
  • The schema only checks. The stored value stays what the editor typed, even when the schema transforms it.
  • It runs on the server only.

Where checks run

RuleIn the adminOn the server
required and the built-in checksYesYes
condition and a plain defaultYesYes
validate, a function default, hooksNoYes
uniqueNoYes

Errors of the server show at the field in the admin. Over HTTP, all errors of one write come back in one 400 response.

Conditions

condition shows a field only when other values match. This example shows a field for each kind of media:

fields: {
  mediaType: { type: "select", options: ["image", "video", "youtube"] },
  image: { type: "image", condition: { mediaType: "image" } },
  url: { type: "text", required: true, condition: { mediaType: "youtube" } },
  caption: { type: "text", condition: { mediaType: { ne: "youtube" } } },
}

Here the editor picks youtube. The admin shows url and hides image and caption. The required check of url applies only now.

Match rules

  • A plain value means "equals". The operators are eq, ne, in, notIn, gt, gte, lt, lte and exists.
  • Several keys MUST all match.
  • $some matches when one condition in a list matches. $every matches when all do.
  • A key names a field next to this one: in the same document, group or list item.
  • Start a key with / to name a field from the document root, like "/settings.mode".
fields: {
  mediaType: { type: "select", options: ["image", "video", "youtube"] },
  forSale: { type: "boolean" },
  price: { type: "number", condition: { forSale: true } },
  note: { type: "text", condition: { price: { gt: 0 } } },
  // One of several values.
  player: {
    type: "text",
    condition: { $some: [{ mediaType: "video" }, { mediaType: "youtube" }] },
  },
  // A field in a group or list item reads a field of the document root.
  badge: { type: "text", condition: { "/forSale": true } },
}

What a false condition does

  • The admin hides the field and saves it as null.
  • The server skips required and the other checks for it.
  • Reads ignore conditions and return what is stored.

condition is JSON, because the admin evaluates it in the browser. For logic that needs code, use validate or a hook.