Sanity Studio uses schemas to define the structure and behavior of content types in your CMS. Creating custom schemas is essential when the default types don't match your content model. By defining custom fields, validation rules, and document types, you ensure your data is structured precisely for your project’s needs.

Create a New Document Type

Create and open a new file in your Studio’s schemaTypes folder called eventType.ts. Copy-paste the following code into it:

code
import {defineField, defineType} from 'sanity'
code

code
export const eventType = defineType({
code
name: 'event',
code
title: 'Event',
code
type: 'document',
code
fields: [
code
defineField({
code
name: 'name',
code
type: 'string',
code
}),
code
],
code
})

Note: The defineField and defineType helper functions in the code above are not required, but they provide autocomplete suggestions and can catch errors in your configuration in code editors with TypeScript tooling.

Now you can import this document type into the schemaTypes array in the index.ts file in the same folder.

code
import {eventType} from './eventType'
code
code
export const schemaTypes = [eventType]
code

When you save these two files, your Studio should automatically reload and show your first document type. You can and should create a new “event” document.

Article image

Note: When you add content in the Name field, all your changes are automatically synced to your project's dataset in the Content Lake.

Add some more document types with fields in them. Same procedure as with the event type: add new files, copy-paste the code into them, and import and add them to the schemaType array in index.ts.

code
import {defineField, defineType} from 'sanity'
code
code
export const artistType = defineType({
code
name: 'artist',
code
title: 'Artist',
code
type: 'document',
code
fields: [
code
defineField({
code
name: 'name',
code
type: 'string',
code
}),
code
defineField({
code
name: 'description',
code
type: 'text',
code
}),
code
defineField({
code
name: 'photo',
code
type: 'image',
code
}),
code
],
code
})
code
code
import {defineField, defineType} from 'sanity'
code

code
export const venueType = defineType({
code
name: 'venue',
code
title: 'Venue',
code
type: 'document',
code
fields: [
code
defineField({
code
name: 'name',
code
type: 'string',
code
}),
code
defineField({
code
name: 'city',
code
type: 'string',
code
}),
code
defineField({
code
name: 'country',
code
type: 'string',
code
}),
code
],
code
})
code

Notice how all these document types use singular names and titles. This is because the singular form makes sense in contexts where these values are used.

code
import {eventType} from './eventType'
code
import {artistType} from './artistType'
code
import {venueType} from './venueType'
code
code
export const schemaTypes = [artistType, eventType, venueType]
code

Confirm in your Sanity Studio that you can create new Artist, Event and Venue type documents.

Article image

Adding Familiar Field Types

Sanity Studio has the field types you'd expect for storing content in a JSON format. For example string, number, boolean, array, object, and more.

In a typical project, the document types you create and the fields you add within them should be informed by conversations you've had with designers and content creators.

Add the following fields to your event schema type. You will extend the configuration later to make their purpose clearer:

Once complete, your eventType file should look like this:

code
import {defineField, defineType} from 'sanity'
code
code
export const eventType = defineType({
code
name: 'event',
code
title: 'Event',
code
type: 'document',
code
fields: [
code
defineField({
code
name: 'name',
code
type: 'string',
code
}),
code
defineField({
code
name: 'slug',
code
type: 'slug',
code
}),
code
defineField({
code
name: 'eventType',
code
type: 'string',
code
}),
code
defineField({
code
name: 'date',
code
type: 'datetime',
code
}),
code
defineField({
code
name: 'doorsOpen',
code
type: 'number',
code
}),
code
defineField({
code
name: 'venue',
code
type: 'reference',
code
to: [{type: 'venue'}],
code
}),
code
defineField({
code
name: 'headline',
code
type: 'reference',
code
to: [{type: 'artist'}],
code
}),
code
defineField({
code
name: 'image',
code
type: 'image',
code
}),
code
defineField({
code
name: 'details',
code
type: 'array',
code
of: [{type: 'block'}],
code
}),
code
defineField({
code
name: 'tickets',
code
type: 'url',
code
}),
code
],
code
})
code

You can now compose and publish documents with multiple fields of varying data types, including a "reference" field that can relate one document with another. You could deploy this to content creators in its current state. It’s a fully-functioning content management system!

The Details Field as “block content”

You might notice that the details field appears as a block content (or "rich text") editor in the Studio. Any array type field that includes a block type will automatically change the UI for the field to this editor.

This is how Sanity Studio is designed for authoring and storing block content. Instead of saving block content and rich text in formats like Markdown or HTML as a string, Sanity Studio stores it in the open-source specification called Portable Text. This unlocks powerful querying and filtering capabilities in your projects and makes integrating across most platforms and frameworks easier.

Import Some Content

In the following lessons you'll query and render content from this dataset. You could painstakingly hand-craft individual documents in Sanity Studio. Or you can import this test dataset using Sanity CLI.

code
# inside /apps/studio
code
pnpm dlx sanity@latest dataset import production.tar.gz production
code

A successful import will give you a bunch of artists, venues, and events in the past and future between 2010–2030.

Reminder: All the documents have now been written to the Content Lake, and you are browsing them in your locally configured Sanity Studio.

Your multiplayer, real-time dashboard for authoring content is presently stuck on your computer. Time to share it with the world, and your authors, in the next lesson.