Troubleshooting adding a schema to Sanity project

23 replies
Last updated: Jun 3, 2023
Hi everyone,
I’m following the tutorial on getting my first schema set up, and I copied and pasted the example schema for pet into the schemas folder, but Sanity is telling me I haven’t provided a type or a title…. any thoughts on what I might be doing wrong?
AI Update

Ah, this is a common issue! The problem is likely that you need to export the schema and register it in your Sanity configuration file. Just creating the file in the schemas folder isn't enough - Sanity won't automatically pick it up.

Here's what you need to do:

1. Make sure your schema file exports the type

Your pet schema file should look something like this:

import { defineType, defineField } from 'sanity'

export const pet = defineType({
  name: 'pet',
  type: 'document',
  title: 'Pet',
  fields: [
    defineField({
      name: 'name',
      type: 'string',
      title: 'Name',
    }),
    // other fields...
  ],
})

Notice the export keyword before the schema definition - this is crucial!

2. Import and register it in your sanity.config.ts (or sanity.config.js)

In your main Sanity config file, you need to import your schema and add it to the schema.types array:

import { defineConfig } from 'sanity'
import { pet } from './schemas/pet' // adjust path as needed

export default defineConfig({
  // ... other config
  schema: {
    types: [pet], // Add your schema here
  },
})

If you have an index file in your schemas folder (like schemas/index.ts), you might need to export it from there first:

// schemas/index.ts
export { pet } from './pet'

Then import it in your config:

import { pet } from './schemas'

The error you're seeing happens because Sanity's schema system is looking for registered schema types in your config but can't find yours. The defineType helper provides TypeScript support and autocomplete, but the schema still needs to be properly exported and registered in your config for Sanity to recognize it.

After making these changes, restart your dev server and the error should disappear!

Hi Gary!
Sounds like it hasn’t been added to the either a primary schema file or to the
sanity.config.ts
file.
Here’s a project I’m working on:
I organize my schema files by type:

File - sanity.config.ts

export default defineConfig({

...

schema: {

types: [ ...siteSettings, ...navigation, ...objects, ...documents, ...taxonomies]

},


Here’s an example of a starter’s sanity.config.ts:

File - sanity.config.ts


export default defineConfig({

...

//edit schemas in './sanity/schema'

schema,

...



File - schema.ts:

import { SchemaTypeDefinition } from 'sanity'

import post from "./post";


export const schema: { types: SchemaTypeDefinition[] } = {

types: [

post

],

}
Hi Ryan!
I
think I’m doing the same basic thing you have in your code above but on a smaller scale. I’ll share my files below:
Feel free to post them as a private gist if it’s easier.
schemas/pet.ts
export default {
  name: 'pet',
  type: 'document',
  title: 'Pet',
  fields: [
    {
      name: 'name',
      type: 'string',
      title: 'Name'
    }
  ]
}
schemas/index.ts
import pet from './pet'

export const schemaTypes = ['pet']
That’s all I have so far… let me know if you want a screen grab of the errors
They basically say “unnamed_type_index_0”
Do you have this added to your config?
Yup
Let’s see that section
One sec…
export default defineConfig({
  name: 'default',
  title: 'indigo-elephant',

  projectId: '3uxw0mxz',
  dataset: 'production',

  plugins: [deskTool(), visionTool(), ...(isDev ? devOnlyPlugins : [])],

  schema: {
    types: [...schemaTypes],
  },
})
And schemaTypes are imported correctly
also tried:
types: schemaTypes (without destructuring)
One sec.
Try removing the quotes from [‘pet’]
in your index.ts
well that’s embarrassing….
No. That’s everyones everyday with a quote somewhere.
I’m going to check the sanity docs… I could have sworn it was in quotes there also… my pride is at stake here, lol
LMK if it’s in the docs. Glad you are onto the next step:)
Nope, the docs were correct… thanks very much for the help!!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?