Schema types
Schema Types are the way Sanity has of blueprinting what each document type will contain.
Creating a new Schema Type
Section titled “Creating a new Schema Type”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:
schemaTypes folder
Section titled “schemaTypes folder”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.
A normal type file
Section titled “A normal type file”This is how our pages type file looks like:
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.
index.js file
Section titled “index.js file”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:
import pages from './contentTypes/pages'import solutions from './contentTypes/solutions'...export const schemaTypes = [ pages, solutions, ...]sanity.config.js
Section titled “sanity.config.js”In this file we tell Sanity where our types are.
import {schemaTypes} from './schemaTypes'export default defineConfig({ ... schema: { types: schemaTypes, }, ...})