
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeLooking at your code, you're trying to ensure that only one header and one footer can be added to the array. The Rule.unique() method you're using is actually for ensuring array items are unique within that array, but what you want is to limit each type to appearing only once.
Here's the correct approach - use custom validation on the array itself:
export default defineType({
title: 'Component Builder',
name: 'pageBuilder',
type: 'array',
of: [
{
type: 'header',
},
{
type: 'footer',
},
],
validation: Rule => Rule.custom((items) => {
if (!items) return true;
const headerCount = items.filter(item => item._type === 'header').length;
const footerCount = items.filter(item => item._type === 'footer').length;
if (headerCount > 1) {
return 'Only one header is allowed';
}
if (footerCount > 1) {
return 'Only one footer is allowed';
}
return true;
})
})This custom validation function:
_type to count how many of each type existtrue to indicate the validation passedThe validation will run whenever someone tries to add items to the array and will prevent publishing if they try to add multiple headers or footers. The editor will see a clear error message telling them what the limit is.
Note that Rule.unique() is meant for ensuring values within an array don't duplicate (like ensuring all items in a tags array are different), not for limiting how many times a specific type appears. That's why you need the custom validation approach shown above.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store