Fields
Each key in fields is one field. Its type says what it stores and which input the admin shows:
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
| Type | Stores |
|---|---|
array | A list of items with the same fields |
blocks | A list of page sections of different types |
boolean | true or false |
date, datetime | A calendar day, or a moment in UTC |
file | Any file from the media library |
group | Fields nested under one key |
image | An image from the media library |
link | A link to a document, a URL or a file |
number | A number |
relation, join | Ids of other documents, and the reverse side |
richtext | A structured document with formatting |
select | One or more of the listed options |
size | A number with a CSS unit, { value, unit } |
slug | A URL part, like about-us |
text, textarea | A 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.
({ user }) => user?.id, runs on the server only.false shares this field between languages. Only on top-level fields. See Limits
defaultapplies only when a document is created, and only to fields that are missing.required,default,hiddenandprivatedo not apply tojoin, 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.
| Goal | Use |
|---|---|
| Keep a value out of the admin form | hidden |
| Show a value, but block edits in admin | readOnly |
| Keep a value out of public reads | private |
| Decide who can read or write a value | access |
Admin options
These options change how the admin form shows a field. The stored document keeps its shape, and the API ignores them.
"full".tabs on the collection, global, group or list item.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:
| Type | TypeScript type |
|---|---|
text, textarea, slug | string |
date, datetime | string, as ISO text |
number | number |
boolean | boolean |
select | The union of the options, or an array of it with many |
relation, image, file | An id string, or string[] with many |
size | { value, unit }, with the units as a union |
group | An object of its fields |
array | A list of objects of its fields, each with an id |
blocks | A list of objects, told apart by blockType |
richtext, link | The 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
requiredremovesnullandundefinedfrom the type. Without it, a field can also benullor missing.populatereplaces ids with documents, andselectdrops 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.
"\\d{5}".min (or 0) plus a whole multiple of step, like the HTML step attribute.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:
| Key | Holds |
|---|---|
siblingData | The values next to this field: same document, group or list item |
data | The whole document |
originalDoc | The stored document. Empty on create |
operation | The write: create, update or setGlobal |
cms | The local API. Use it for lookups |
user | The 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. Addrequiredto 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
| Rule | In the admin | On the server |
|---|---|---|
required and the built-in checks | Yes | Yes |
condition and a plain default | Yes | Yes |
validate, a function default, hooks | No | Yes |
unique | No | Yes |
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,lteandexists. - Several keys MUST all match.
$somematches when one condition in a list matches.$everymatches 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
requiredand 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.