Guides

Fields

The field helpers that describe the data of bricks and collections.

Schemas are built with fields.*. Each helper returns a Valibot schema that validates the value, fills in a default when it's missing, and carries a description that the admin turns into a form input. The set is deliberately small.

Every helper takes a label, an optional help text shown under the input, and an optional default. A brick field can also be private: true: only the server reads it (say, where a form's messages go), so an interactive brick leaves it out of what it sends to the browser. The admin and the brick's action still see the stored value.

Helper Stores Options
fields.text a one-line string required, maxLength, placeholder
fields.textarea a multi-line string required, maxLength, placeholder
fields.number a finite number min, max, step
fields.boolean true or false
fields.select one of a fixed set of values options: value → label; the first is the default
fields.url an http(s) URL or a path such as /about required, placeholder
fields.image { src, alt, title, asset }: an uploaded image or a URL required
fields.file { src, title, asset }: an uploaded file (video, audio, PDF…) or a URL accept (kinds, default all), required
fields.date a date as YYYY-MM-DD, or '' required
fields.list an ordered list of items of (the item's field), min, max
fields.object a group of named fields fields
fields.reference the id of a document in another collection collection, required

Examples

import { fields } from '@mortar-kit/core';

const schema = {
  title: fields.text({ label: 'Title', required: true, maxLength: 120 }),
  price: fields.number({ label: 'Price', min: 0, step: 0.01 }),
  align: fields.select({ label: 'Alignment', options: { left: 'Left', center: 'Centre' } }),
  cover: fields.image({ label: 'Cover image' }),
  trailer: fields.file({ label: 'Trailer', accept: ['video'] }),
  published: fields.date({ label: 'Published', required: true, default: '2026-01-01' }),
  category: fields.reference({ label: 'Category', collection: 'categories', required: true }),
  links: fields.list({
    label: 'Links',
    of: fields.object({ label: 'Link', fields: { label: fields.text({ label: 'Label' }), href: fields.url({ label: 'URL' }) } }),
  }),
};

Notes

  • Links are checked. url, image and file accept http(s) URLs, site paths (/, #, ?) and mailto: or tel: links, and reject other schemes such as javascript:, because the values end up in href and src.
  • Uploads store a key, not a URL. asset holds the uploaded file's key and src is filled in when content is read, so render src and moving files to another bucket keeps every link. See Assets.
  • Dates sort as strings. YYYY-MM-DD is stored as written, so a collection can sort by it.
  • References store ids, not slugs. Renaming a document keeps every link to it. Deleting a document that others reference is refused, and the admin shows where it is used.
  • Select values must not look like integers ('1', '2'): JavaScript reorders such keys. Use names like c1.

Types follow the schema: InferFields<typeof schema> is the data's type, and bricks and collections infer it for you.

fieldsvariable

import { fields } from '@mortar-kit/core'

const fields: { text, textarea, number, boolean, select, url, image, file, list, object, date, reference }

Field helpers used to declare the schema of a brick or a collection.

Members

text: (o: TextOptions) => Field<string>
textarea: (o: TextOptions) => Field<string>
number: (o: Common<number> & { min?: number; max?: number; step?: number }) => Field<number>
boolean: (o: Common<boolean>) => Field<boolean>
select: (o: Common<keyof O & string> & { options: O }) => Field<keyof O & string>
url: (o: Common<string> & { placeholder?: string; required?: boolean }) => Field<string>
image: (o: Common<Partial<Image>> & { required?: boolean }) => Field<Image>
file: (o: Common<Partial<FileValue>> & { accept?: readonly AssetKind[]; required?: boolean }) => Field<FileValue>
list: (o: Common<T[]> & { of: Field<T>; min?: number; max?: number }) => Field<T[]>
object: (o: Common<InferFields<F>> & { fields: F }) => Field<InferFields<F>>
date: (o: Common<string> & { required?: boolean }) => Field<string>
reference: (o: Common<string> & { collection: string; required?: boolean }) => Field<string>

InferFieldstype

import { InferFields } from '@mortar-kit/core'

type InferFields<F extends Fields> = { -readonly [K in keyof F]: v.InferOutput<F[K]> }

The data type described by a record of fields.