Sanity array field with custom object type reference
Your schema looks good! This defines an array field named contacts that will hold multiple items of type contact_person.
Here's what your schema does:
- name:
'contacts'- the field identifier used in queries and code - title:
'Contacts'- the human-readable label shown in Sanity Studio - type:
'array'- makes this an orderable collection - of:
[{ type: 'contact_person' }]- specifies that array items must be of typecontact_person
The array field will automatically give you drag-and-drop reordering, and each item will get a unique _key property that Sanity uses to track items. You'll also get copy/paste functionality for free, letting editors copy individual contacts or entire arrays between documents.
Make sure you have a contact_person schema type defined elsewhere in your schema files. It could be either a document type or an object type, depending on whether you want contacts to be standalone documents (referenced) or embedded objects:
// As an embedded object (inline)
{
name: 'contact_person',
type: 'object',
fields: [
{ name: 'name', type: 'string' },
{ name: 'email', type: 'string' },
// ... other fields
]
}
// Or as a reference to a document
{
name: 'contacts',
type: 'array',
of: [
{
type: 'reference',
to: [{ type: 'contact_person' }]
}
]
}If you're getting a validation error about invalid "of" property, double-check that contact_person is properly defined and exported in your schema.
Show original thread43 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.