Array with multiple reference types causing schema validation error

12 replies
Last updated: Jan 28, 2026
Is it possible to have a
type: array
, of more than one entry for
type: reference
? I kept getting an error that pointed to https://www.sanity.io/help/schema-array-of-invalid when I tried to make multiple
{type: 'reference', to: [a single type]}
. I tried uniquely `name`ing them on both the
to
values, and the reference type itself, but it's hung up on the fact I have dozens of
reference
types in the
type: 'array'
.
Currently reverted back to just a single
type: 'reference'
with a lot of
to
values, but it's problematic because of the many types I have, many of them have hundreds of results, making it very difficult for me to say I want "this document" from "this type"
The generated groq from that on dash is quite huge too since it has to match on a ton of fields on each type
😐
AI Update

Yes, you can have an array with multiple reference types! The error you're hitting is because Sanity requires each item in the of array to be uniquely identifiable.

The Problem

When you have multiple entries in the of array with the same type, Sanity can't tell them apart unless you give them unique name properties. From the Sanity documentation on array schemas, this won't work:

{
  type: 'array',
  name: 'items',
  of: [
    { type: 'reference', to: [{type: 'author'}] },
    { type: 'reference', to: [{type: 'book'}] },  // Both are type: 'reference'!
  ]
}

The Solution

Add a unique name property to each reference definition:

{
  type: 'array',
  name: 'items',
  of: [
    {
      type: 'reference',
      name: 'authorReference',  // Unique name
      to: [{type: 'author'}],
      title: 'Author'
    },
    {
      type: 'reference',
      name: 'bookReference',  // Different unique name
      to: [{type: 'book'}],
      title: 'Book'
    },
    {
      type: 'reference',
      name: 'articleReference',
      to: [{type: 'article'}],
      title: 'Article'
    }
    // Add as many as you need!
  ]
}

Each array item will have its _type set to the name you specified (e.g., authorReference, bookReference), which allows Sanity to distinguish between them.

Why This Helps Your Use Case

You mentioned that having a single reference with many to values makes it hard to find specific documents because hundreds of results appear. By splitting them into separate named references, you can:

  1. Group by type - Each reference type appears as a separate option in the Studio UI
  2. Better UX - Users select the type of reference first, then search within that specific document type
  3. Cleaner queries - Your GROQ queries will be more manageable since each reference has a distinct _type

The array will store data like:

[
  {"_type": "authorReference", "_ref": "author-123"},
  {"_type": "bookReference", "_ref": "book-456"}
]

This approach gives you dozens of reference types in a single array while maintaining a clean, organized interface!

Show original thread
12 replies

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?