Skip to content

Field Types

Contentrain provides 27 field types for defining model schemas. Each type includes built-in validation, constraints, and serialization rules.

ts
type FieldType =
  | 'string' | 'text' | 'email' | 'url' | 'slug' | 'color' | 'phone' | 'code' | 'icon'
  | 'markdown' | 'richtext'
  | 'number' | 'integer' | 'decimal' | 'percent' | 'rating'
  | 'boolean'
  | 'date' | 'datetime'
  | 'image' | 'video' | 'file'
  | 'relation' | 'relations'
  | 'select' | 'array' | 'object'

Field Definition Schema

Every field in a model is defined using the FieldDef interface:

ts
interface FieldDef {
  type: FieldType          // Required: the field type
  required?: boolean       // Validation: must have a value
  unique?: boolean         // Validation: unique within the collection
  default?: unknown        // Default value when not provided
  min?: number             // Minimum (length for strings, value for numbers)
  max?: number             // Maximum (length for strings, value for numbers)
  pattern?: string         // Regex validation pattern
  options?: string[]       // Allowed values (for select type)
  model?: string | string[] // Target model(s) for relation types
  items?: string | FieldDef // Item type for array fields
  fields?: Record<string, FieldDef> // Sub-fields for object type
  accept?: string          // MIME types for media fields
  maxSize?: number         // Max file size in bytes for media fields
  description?: string     // Human-readable field description
}

Complete Field Type Reference

Text Group

Field types for textual content of varying lengths and formats.

TypeDescriptionConstraintsStorage
stringShort text, single linemin, max (length), patternstring
textMulti-line plain textmin, max (length)string
emailEmail addressAuto-validated formatstring
urlWeb URLAuto-validated formatstring
slugURL-friendly identifierLowercase, hyphens, no spacesstring
codeSource code or preformatted textmin, max (length)string
iconIcon identifier (e.g., icon library name)pattern, optionsstring

Examples

json
{
  "title": {
    "type": "string",
    "required": true,
    "min": 1,
    "max": 200
  },
  "bio": {
    "type": "text",
    "max": 1000,
    "description": "Author biography"
  },
  "contact_email": {
    "type": "email",
    "required": true
  },
  "website": {
    "type": "url"
  },
  "post_slug": {
    "type": "slug",
    "required": true,
    "unique": true
  },
  "snippet": {
    "type": "code"
  },
  "category_icon": {
    "type": "icon",
    "options": ["home", "settings", "user", "star"]
  }
}

Rich Content Group

Field types for formatted content with markup.

TypeDescriptionConstraintsStorage
markdownMarkdown-formatted textmin, max (length)string
richtextHTML/rich text contentmin, max (length)string

Examples

json
{
  "content": {
    "type": "markdown",
    "required": true,
    "description": "Article body in Markdown format"
  },
  "formatted_bio": {
    "type": "richtext",
    "max": 5000
  }
}

TIP

For document-kind models, use the body key in content data for the main Markdown content rather than a markdown field. The markdown field type is for additional rich text fields within any model kind.

Numeric Group

Field types for numbers with different precision and display semantics.

TypeDescriptionConstraintsStorage
numberGeneral number (integer or float)min, max (value)number
integerWhole number onlymin, max (value)number
decimalFloating-point numbermin, max (value)number
percentPercentage value (0-100)min, max (value, defaults 0-100)number
ratingStar/score ratingmin, max (value, e.g., 1-5)number

Examples

json
{
  "price": {
    "type": "decimal",
    "required": true,
    "min": 0
  },
  "quantity": {
    "type": "integer",
    "min": 0,
    "max": 10000,
    "default": 0
  },
  "discount": {
    "type": "percent",
    "min": 0,
    "max": 100
  },
  "user_rating": {
    "type": "rating",
    "min": 1,
    "max": 5
  },
  "sort_order": {
    "type": "number",
    "default": 0
  }
}

Boolean

Simple true/false toggle.

TypeDescriptionConstraintsStorage
booleanTrue or falsedefaultboolean

Example

json
{
  "is_featured": {
    "type": "boolean",
    "default": false
  },
  "published": {
    "type": "boolean",
    "required": true
  }
}

Date/Time Group

Field types for temporal data.

TypeDescriptionConstraintsStorage
dateDate only (no time)min, max (ISO date strings)string (ISO 8601)
datetimeFull date and timemin, max (ISO datetime strings)string (ISO 8601)

Examples

json
{
  "publish_date": {
    "type": "date",
    "required": true
  },
  "event_time": {
    "type": "datetime"
  }
}

INFO

Dates are stored as ISO 8601 strings: "2026-03-15" for date, "2026-03-15T14:30:00Z" for datetime.

Media Group

Field types for file uploads and media references.

TypeDescriptionConstraintsStorage
imageImage file referenceaccept, maxSizestring (path)
videoVideo file referenceaccept, maxSizestring (path)
fileGeneric file referenceaccept, maxSizestring (path)

Examples

json
{
  "cover_image": {
    "type": "image",
    "required": true,
    "accept": "image/png,image/jpeg,image/webp",
    "maxSize": 5242880,
    "description": "Cover image (max 5MB)"
  },
  "intro_video": {
    "type": "video",
    "accept": "video/mp4,video/webm",
    "maxSize": 52428800
  },
  "resume": {
    "type": "file",
    "accept": "application/pdf",
    "maxSize": 10485760
  }
}

TIP

Media paths reference files in the project's assets_path (configured in config.json). The accept field uses standard MIME types.

Color

Color value field.

TypeDescriptionConstraintsStorage
colorColor valuepattern (e.g., hex format)string

Example

json
{
  "brand_color": {
    "type": "color",
    "required": true,
    "pattern": "^#[0-9a-fA-F]{6}$",
    "default": "#000000"
  }
}

Phone

Phone number field.

TypeDescriptionConstraintsStorage
phonePhone numberpatternstring

Example

json
{
  "contact_phone": {
    "type": "phone",
    "pattern": "^\\+[1-9]\\d{1,14}$",
    "description": "E.164 format phone number"
  }
}

Relation Group

Field types for connecting entries across models.

TypeDescriptionConstraintsStorage
relationSingle relation to another entrymodel (target model ID)string (entry ID)
relationsMultiple relations to other entriesmodel (target model ID or array)string[] (entry IDs)

Examples

json
{
  "author": {
    "type": "relation",
    "required": true,
    "model": "team-members",
    "description": "Post author"
  },
  "tags": {
    "type": "relations",
    "model": "tags",
    "description": "Associated tags"
  },
  "related_content": {
    "type": "relations",
    "model": ["blog-posts", "case-studies"],
    "description": "Related items from multiple collections"
  }
}

WARNING

Relation fields store entry IDs as references. Use the resolve parameter in contentrain_content_list to expand relations to full entry data at query time.

Complex Group

Field types for structured and compound data.

TypeDescriptionConstraintsStorage
selectSingle choice from predefined optionsoptions (required)string
arrayOrdered list of itemsitems (type or FieldDef), min, maxarray
objectNested object with sub-fieldsfields (Record of FieldDef)object

Examples

json
{
  "status": {
    "type": "select",
    "required": true,
    "options": ["active", "inactive", "coming-soon"],
    "default": "active"
  },
  "features": {
    "type": "array",
    "items": "string",
    "min": 1,
    "max": 10,
    "description": "List of feature names"
  },
  "gallery": {
    "type": "array",
    "items": {
      "type": "object",
      "fields": {
        "src": { "type": "image", "required": true },
        "caption": { "type": "string" },
        "alt": { "type": "string", "required": true }
      }
    }
  },
  "seo": {
    "type": "object",
    "fields": {
      "meta_title": { "type": "string", "max": 60 },
      "meta_description": { "type": "text", "max": 160 },
      "og_image": { "type": "image" },
      "no_index": { "type": "boolean", "default": false }
    }
  }
}

INFO

The array type can hold simple values (items: "string") or complex objects (items: { type: "object", fields: {...} }). The object type always requires a fields definition.

Complete Type Summary Table

#TypeGroupJS StorageSupports min/maxSupports patternSupports optionsSupports modelSupports items/fields
1stringTextstringLengthYesNoNoNo
2textTextstringLengthNoNoNoNo
3emailTextstringNoAutoNoNoNo
4urlTextstringNoAutoNoNoNo
5slugTextstringLengthAutoNoNoNo
6codeTextstringLengthNoNoNoNo
7iconTextstringNoYesYesNoNo
8markdownRich ContentstringLengthNoNoNoNo
9richtextRich ContentstringLengthNoNoNoNo
10numberNumericnumberValueNoNoNoNo
11integerNumericnumberValueNoNoNoNo
12decimalNumericnumberValueNoNoNoNo
13percentNumericnumberValueNoNoNoNo
14ratingNumericnumberValueNoNoNoNo
15booleanBooleanbooleanNoNoNoNoNo
16dateDate/TimestringDate rangeNoNoNoNo
17datetimeDate/TimestringDatetime rangeNoNoNoNo
18imageMediastringNoNoNoNoNo
19videoMediastringNoNoNoNoNo
20fileMediastringNoNoNoNoNo
21colorColorstringNoYesNoNoNo
22phonePhonestringNoYesNoNoNo
23relationRelationstringNoNoNoYesNo
24relationsRelationstring[]NoNoNoYesNo
25selectComplexstringNoNoYes (required)NoNo
26arrayComplexarrayCountNoNoNoitems
27objectComplexobjectNoNoNoNofields

Constraint Properties Reference

PropertyApplicable TypesDescription
requiredAllField must have a non-null value
uniqueAll (collection only)Value must be unique across all entries
defaultAllDefault value when field is not provided
minstring, text, code, slug, markdown, richtext, number, integer, decimal, percent, rating, arrayMinimum length (strings) or value (numbers) or count (arrays)
maxstring, text, code, slug, markdown, richtext, number, integer, decimal, percent, rating, arrayMaximum length (strings) or value (numbers) or count (arrays)
patternstring, icon, color, phoneRegular expression for validation
optionsselect, iconArray of allowed string values
modelrelation, relationsTarget model ID(s) for the relation
itemsarrayItem type: a type name string or a nested FieldDef
fieldsobjectSub-field definitions as Record<string, FieldDef>
acceptimage, video, fileComma-separated MIME types
maxSizeimage, video, fileMaximum file size in bytes
descriptionAllHuman-readable description of the field

Released under the MIT License.