Skip to content

Schema types

Schema Types are the way Sanity has of blueprinting what each document type will contain.

There are several ways to create and let Sanity know that we have created a new Schema Type, this is how we do it in this project:

This folder contains all our types files. Each type needs a separate file. We store all of our regular content types in the contentTypes folder, and we have a siteSettings separate folder to store only our settings, for organization purposes.

This is how our pages type file looks like:

schemaTypes/contentTypes/pages.js
import { metaAttributes } from "@attributes/meta_attributes.js";
import { title } from "@island/title.js";
import { modules } from "@modules/common/index.js";
import { heros } from "@modules/hero";
export default {
name: "pages",
title: "Pages",
type: "document",
fields: [
...title(),
...metaAttributes({ showImage: false, showHeaderFooter: { header: true, footer: true } }),
...heros(),
...modules(),
],
preview: {
select: {
title: "title.en",
subtitle: "attributes.slug",
},
prepare(selection) {
return {
title: selection.title,
subtitle: selection.subtitle,
};
},
},
};

We can directly export the object with the type or we could also import the defineType method from Sanity and use that.

We do export most of our type objects directly, and those are picked up by our index.js file in our schemaTypes folder.

This file is just a barrel file that imports all types and exports a general schemaTypes array that will be picked up by our sanity.config.ts file.

This is a sample of how that file looks like:

schemaTypes/index.js
import pages from './contentTypes/pages'
import solutions from './contentTypes/solutions'
...
export const schemaTypes = [
pages,
solutions,
...
]

In this file we tell Sanity where our types are.

sanity.config.ts
import {schemaTypes} from './schemaTypes'
export default defineConfig({
...
schema: {
types: schemaTypes,
},
...
})