Error when adding custom object to block content annotations in Sanity V3

1 replies
Last updated: Sep 26, 2022
Hello!I’m editing the annotations in the block content to support external links and other custom objects in the new V3.
Since I’m using GraphQL, I can’t have any inline objects and all objects need to be defined globally. So I created a type externalLink as:


export default {
  name: 'externalLink',
  title: 'External Link',
  type: 'object',
  liveEdit: false, 
  fields: [
    {
      title: 'URL',
      name: 'href',
      type: 'url',
    },
  ]
}
I added this type to the schema. but when I want to place it in the annotations of my block Content, I get the following error:


Error: Could not resolve jsonType of undefined. No parent type found
Error: Could not resolve jsonType of undefined. No parent type found
    at isJSONTypeOf (<http://localhost:3333/node_modules/.vite/deps/chunk-TA2UENNL.js?v=47494152:83241:11>)
    at <http://localhost:3333/node_modules/.vite/deps/chunk-TA2UENNL.js?v=47494152:83366:24>
    at Array.map (<anonymous>)
    at validateAnnotations (<http://localhost:3333/node_modules/.vite/deps/chunk-TA2UENNL.js?v=47494152:83357:22>)
    at validateMarks (<http://localhost:3333/node_modules/.vite/deps/chunk-TA2UENNL.js?v=47494152:83306:19>)
    at validateBlockType (<http://localhost:3333/node_modules/.vite/deps/chunk-TA2UENNL.js?v=47494152:83271:13>)
    at visitors.reduce._problems (<http://localhost:3333/node_modules/.vite/deps/chunk-TA2UENNL.js?v=47494152:83585:19>)
    at Array.reduce (<anonymous>)
    at <http://localhost:3333/node_modules/.vite/deps/chunk-TA2UENNL.js?v=47494152:83584:21>
    at <http://localhost:3333/node_modules/.vite/deps/chunk-TA2UENNL.js?v=47494152:83599:40>

And this is my BlockEditor code:

export default {
  title: 'Block Editor',
  name: 'blockEditor',
  type: 'array',
  of: [
    {
      title: 'Block',
      type: 'block',
      styles: [
        {title: 'Normal', value: 'normal'},
        {title: 'H2', value: 'h2'},
        {title: 'H3', value: 'h3'},
        {title: 'Quote', value: 'blockquote'},
      ],
      lists: [{title: 'Bullet', value: 'bullet'}],
      marks: {
        decorators: [
          {title: 'Strong', value: 'strong'},
          {title: 'Emphasis', value: 'em'},
        ],
        annotations: [
          {
            type: 'externalLink'
          }
        ],
      },
    },
    {
      type: 'image',
      name: 'image',
      options: {
        hotspot: true,
      }
    },
    {
      name: 'line',
      title: 'Line',
      type: 'line',
    },
  ],
}

This example shows me that it is possible (in V2), so my question is, what am I doing wrong here?
[I’m using version 3.0.1-dev-preview.0]
AI Update

Looking at this error with your Sanity v3 Studio configuration, the issue is that when you're using GraphQL with Portable Text, you need to define annotation types globally rather than inline within the block configuration.

The error "Could not resolve jsonType of undefined. No parent type found" occurs because the schema compiler can't properly resolve the inline object type definition when it's nested directly in the annotations array.

Here's how to fix it:

Step 1: Define your externalLink as a separate schema type

Keep your externalLink type definition in a separate file:

// schemas/externalLink.ts
export default {
  name: 'externalLink',
  title: 'External Link',
  type: 'object',
  fields: [
    {
      title: 'URL',
      name: 'href',
      type: 'url',
    },
  ]
}

Step 2: Reference it properly in your block configuration

In your blockEditor schema, reference the type by name only:

export default {
  title: 'Block Editor',
  name: 'blockEditor',
  type: 'array',
  of: [
    {
      title: 'Block',
      type: 'block',
      styles: [
        {title: 'Normal', value: 'normal'},
        {title: 'H2', value: 'h2'},
        {title: 'H3', value: 'h3'},
        {title: 'Quote', value: 'blockquote'},
      ],
      lists: [{title: 'Bullet', value: 'bullet'}],
      marks: {
        decorators: [
          {title: 'Strong', value: 'strong'},
          {title: 'Emphasis', value: 'em'},
        ],
        annotations: [
          {
            name: 'externalLink',
            type: 'externalLink'  // Just reference the type name
          }
        ],
      },
    },
    // ... other block types
  ],
}

Step 3: Make sure both types are registered in your schema

In your sanity.config.ts or schema configuration:

import externalLink from './schemas/externalLink'
import blockEditor from './schemas/blockEditor'

export default defineConfig({
  // ... other config
  schema: {
    types: [
      externalLink,  // Register it globally first
      blockEditor,
      // ... other types
    ],
  },
})

The key difference from v2 is that v3 requires stricter type resolution, especially when using GraphQL. By defining the annotation type globally and then referencing it by name (rather than defining it inline), the schema compiler can properly resolve the type hierarchy.

This approach also has the benefit of making the type reusable across multiple block configurations if needed, and it ensures GraphQL can properly generate types for your schema. As noted in the Portable Text Editor configuration docs, when using GraphQL with Sanity, you should hoist annotation types as separate schema types rather than using anonymous inline definitions.

Hey
user M
! We're handling V3 support on Github Discussions .

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?