Skip to content

Field Types

Contentrain supports 27 field types organized into 7 categories. Each field type has specific validation rules, a JSON storage type, and UI representation.

Field Definition Schema

Every field in a model definition follows this schema:

ts
interface FieldDef {
  type: string                    // One of the 27 types
  required?: boolean              // Field is required
  unique?: boolean                // Value must be unique in collection
  min?: number                    // Min length (string), min value (number), min items (array)
  max?: number                    // Max length (string), max value (number), max items (array)
  pattern?: string                // Regex validation pattern
  options?: string[]              // Valid values for select type
  default?: unknown               // Default value (omitted from JSON when matched)
  description?: string            // Human-readable description
  model?: string | string[]       // Relation target model(s)
  items?: string | FieldDef       // Array item type or definition
  fields?: Record<string, FieldDef>  // Nested object fields
  accept?: string                 // MIME type filter for media fields
}

Type Reference

Text Types

TypeJSON StorageDescriptionValidation
stringstringSingle-line textmin/max length, pattern regex
textstringMulti-line plain textmin/max length, pattern regex
markdownstringMarkdown-formatted textmin/max length
richtextstringHTML rich textmin/max length
codestringCode snippetmin/max length
slugstringURL slugLowercase, alphanumeric, hyphens only (/^[a-z0-9]+(?:-[a-z0-9]+)*$/)

Example field definition:

json
{
  "title": {
    "type": "string",
    "required": true,
    "min": 1,
    "max": 200,
    "description": "Blog post title"
  }
}

Number Types

TypeJSON StorageDescriptionValidation
numbernumberAny number (int or float)min/max range
integernumberInteger onlyMust be Number.isInteger(), min/max range
decimalnumberFloating-point numbermin/max range
percentnumberPercentage (0-100)Must be between 0 and 100
ratingnumberStar rating (1-5)Must be integer between 1 and 5

Example:

json
{
  "price": { "type": "decimal", "required": true, "min": 0 },
  "rating": { "type": "rating" },
  "progress": { "type": "percent", "default": 0 }
}

Boolean Type

TypeJSON StorageDescriptionValidation
booleanbooleanTrue/false toggleMust be true or false

Date Types

TypeJSON StorageDescriptionValidation
datestringDate (ISO format)Must be valid date string (Date.parse())
datetimestringDate + time (ISO format)Must be valid datetime string

Example values:

json
{
  "publishDate": "2026-01-15",
  "createdAt": "2026-01-15T12:00:00.000Z"
}

Contact Types

TypeJSON StorageDescriptionValidation
emailstringEmail addressRegex: /^[^\s@]+@[^\s@]+\.[^\s@]+$/ (warning-level)
urlstringURLMust start with http://, https://, or / (warning-level)
phonestringPhone numbermin/max length, pattern regex

Visual Types

TypeJSON StorageDescriptionValidation
colorstringHex color codeMust match /^#[0-9a-fA-F]{6}$/
iconstringIcon identifiermin/max length

Example:

json
{
  "brandColor": "#3B82F6",
  "menuIcon": "icon-[annon--home-2]"
}

Selection Type

TypeJSON StorageDescriptionValidation
selectstringSingle value from optionsMust be one of defined options[]

Example:

json
{
  "status": {
    "type": "select",
    "options": ["draft", "review", "published"],
    "default": "draft",
    "required": true
  }
}

Media Types

TypeJSON StorageDescriptionValidation
imagestringImage file pathMust be a string (file path)
videostringVideo file pathMust be a string (file path)
filestringGeneric file pathMust be a string (file path)

Media field values store the path to the asset in R2 storage:

json
{
  "cover": "media/original/abc123.webp",
  "thumbnail": "media/thumbnail/abc123.webp"
}

The accept property can filter allowed MIME types:

json
{
  "avatar": {
    "type": "image",
    "accept": "image/png,image/jpeg,image/webp"
  }
}

Relation Types

TypeJSON StorageDescriptionValidation
relationstring or objectReference to another entryMust be entry ID, slug, or { model, ref } for polymorphic
relationsstring[]Array of referencesEach item must be string ID/slug

Single Relation

json
{
  "author": {
    "type": "relation",
    "model": "team-members",
    "required": true
  }
}

Value: "a1b2c3d4e5f6" (entry ID for collections) or "john-doe" (slug for documents)

Polymorphic Relation

When model is an array, the value must be an object:

json
{
  "relatedContent": {
    "type": "relation",
    "model": ["blog-post", "page"]
  }
}

Value: { "model": "blog-post", "ref": "a1b2c3d4e5f6" }

Multi-Relation

json
{
  "tags": {
    "type": "relations",
    "model": "tags",
    "min": 1,
    "max": 10
  }
}

Value: ["tag-id-1", "tag-id-2", "tag-id-3"]

Structured Types

TypeJSON StorageDescriptionValidation
objectobjectNested objectValidates nested fields (max 2 levels deep)
arrayarrayArray of itemsmin/max count, validates items type

Object Field

json
{
  "address": {
    "type": "object",
    "fields": {
      "street": { "type": "string", "required": true },
      "city": { "type": "string", "required": true },
      "zip": { "type": "string", "pattern": "^\\d{5}$" }
    }
  }
}

Array Field (Simple)

json
{
  "tags": {
    "type": "array",
    "items": "string",
    "min": 1,
    "max": 20
  }
}

Array Field (Object Items)

json
{
  "socialLinks": {
    "type": "array",
    "items": {
      "type": "object",
      "fields": {
        "platform": { "type": "select", "options": ["twitter", "github", "linkedin"] },
        "url": { "type": "url", "required": true }
      }
    },
    "max": 5
  }
}

System Fields

These fields are managed by Studio and must never be included in save_content data:

FieldDescription
idEntry ID (12-char hex, auto-generated)
slugDocument slug (set via slug parameter, not in data)
statusPublish status (draft, published, archived)
sourceOrigin indicator (mcp-local, studio, etc.)

Validation Summary

CheckApplies ToSeverity
RequiredAll typeserror
UniqueAll types (collection-level)error
Min/Max lengthstring, text, markdown, richtext, code, phoneerror
Min/Max valuenumber, integer, decimalerror
Min/Max itemsarray, relationserror
Pattern (regex)string, text, phoneerror
Options listselecterror
Email formatemailwarning
URL formaturlwarning
Slug formatslugerror
Color formatcolorerror
Date validitydate, datetimeerror
Integer checkinteger, ratingerror
Percent rangepercenterror
Rating rangeratingerror
Relation targetrelation, relationswarning (referential integrity)
Nested validationobject, array (object items)error

Released under the AGPL-3.0 License.