
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeYou can achieve this with a custom validation rule that queries your dataset to check if any other document has isFeatured set to true. Here's how to implement it:
defineField({
name: 'isFeatured',
type: 'boolean',
title: 'Featured',
validation: (Rule) =>
Rule.custom(async (value, context) => {
// Only validate if isFeatured is being set to true
if (!value) return true
// Get the Sanity client
const client = context.getClient({ apiVersion: '2023-01-01' })
// Query for other documents with isFeatured = true
// Exclude the current document using its _id
const query = `*[_type == $type && isFeatured == true && _id != $id][0...2] {
_id,
title
}`
const params = {
type: context.document?._type,
id: context.document?._id || ''
}
const existingFeatured = await client.fetch(query, params)
// If another document is already featured, show an error
if (existingFeatured && existingFeatured.length > 0) {
const doc = existingFeatured[0]
return `Only one document can be featured at a time. "${doc.title || doc._id}" is already featured.`
}
return true
})
})Key points about this implementation:
getClient() access: The validation context provides access to the Sanity client via context.getClient(), which allows you to query your dataset during validation.
Excluding current document: The query uses _id != $id to exclude the current document, so you can save a document that already has isFeatured set to true without triggering a validation error.
Informative error message: The error message tells the editor which document is already featured, making it easy to find and update if needed.
Only validates when true: The validation only runs when isFeatured is true, so it doesn't unnecessarily query the database for every document save.
Important considerations:
_type and field names to match your schema.title in the query with whatever field you want to display in the error message (or just use _id if you don't have a title field).This approach uses custom validation with async operations, which is perfect for cases where you need to validate against other documents in your dataset.
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