Index
Edit

Schema Types

A type, short for "data type", is a classification of how a particular piece of data is used. Types in the Sanity context are divided into Basic types (e.g. String and Boolean) and Custom types (e.g. Geopoint and Movie).

To illustrate the use of types, have a look at the below schema which defines a simple movie database.

{
  name: 'movies',
  types: [
    {
      title: 'Movie',
      name: 'movie',
type: 'object',
fields: [ { title: 'Title', name: 'title', type: 'string', required: true }, { title: 'Slug', name: 'slug', type: 'slug', options: { source: 'title', maxLength: 96, auto: true } }, { title: 'Overview', name: 'overview', type: 'text', description: 'Keep it brief.' }, { title: 'Release date', name: 'releaseDate', type: 'date' }, { title: 'Popularity', name: 'popularity', type: 'number' }, { name: 'poster', title: 'Poster Image', type: 'image' }, { title: 'Cast Members', name: 'castMembers', type: 'array', of: [{type: 'castMember'}] }, { title: 'Crew Members', name: 'crewMembers', type: 'array', of: [{type: 'crewMember'}] } ], }, { title: 'Cast Member', name: 'castMember', type: 'object', fields: [ { title: 'Character Name', name: 'characterName', type: 'string' }, { title: 'Order', name: 'order', type: 'number' }, { title: 'Actor', name: 'person', type: 'reference', to: [{type: 'person'}] } ] }, { title: 'Crew Member', name: 'crewMember', type: 'object', fields: [ { title: 'Department', name: 'department', type: 'string' }, { title: 'Job', name: 'job', type: 'string' }, { title: 'Person', name: 'person', type: 'reference', to: [{type: 'person'}] } ] }, { title: 'Person', name: 'person', type: 'object', fields: [ { title: 'Name', name: 'name', type: 'string', placeholder: 'Will Smith', description: 'Please don\t be like "Smith, Will".' }, { title: 'Image', name: 'image', type: 'image' } ] } ] }

Basic Types

Basic types are the a-priori building blocks of Sanity data. They consist of: String, Number, Boolean, Array and Object. By design, these are also the legal JSON data types. Read on to learn how to use basic types in your schema.

Custom Types

A Custom type is either:

  • A Basic type with constraints applied (e.g. Date, which is just a String with some extra rules)
  • Or, a collection of several other types (e.g. Movie, which might consist of a String type for name and a Date type for birthday)

Sanity ships with various Custom types. When creating your own Custom types, be sure to give them names which are not taken, lest you accidentally override already defined types. Read on to learn how to use the Custom types which ships with Sanity.


Previous: ReferenceNext: Object