
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeI can see the issue with your validation code. There are a few problems that need to be fixed for the uniqueness validation to work properly.
The main issues are:
"${input}" instead of proper template literal syntax with backticksHere's the corrected version:
validation: Rule => Rule.required().custom(async (input, context) => {
const client = context.getClient({apiVersion: '2021-03-25'})
// Get the current document ID to exclude it from the search
const currentDocId = context.document._id
const query = `count(*[_type == "hospitalManagement" && title == $title && _id != $currentId])`
const params = {
title: input,
currentId: currentDocId.replace('drafts.', '') // Remove 'drafts.' prefix if present
}
const count = await client.fetch(query, params)
if (count > 0) {
return 'Hospital name must be unique'
}
return true
})Key changes:
$title and $currentId) instead of string interpolation - this is safer and handles special characters properly_id != $currentId so you don't count the document being editeddrafts. prefix when comparing IDsasync and use await for cleaner codeThe validation will now properly check if another document (excluding the current one) already has the same hospital name.
You can read more about custom validation and asynchronous validation using the client in the Sanity documentation.
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