Sanity v3: Custom slug validation with hooks causing errors after migration
I can see you're running into the classic Studio v3 migration issue with custom slug validation! The problem is that in v3, you can't use React hooks like useClient in regular functions - hooks only work in React components.
Here's how to fix your custom slug validation for Studio v3:
The key change is that the isUnique function (and similar schema functions) receives a context object as the second parameter that contains the client. Here's the corrected approach:
export function isPageSlugUnique(slug, context) {
const { document, getClient } = context;
const client = getClient({ apiVersion: "2022-02-03" });
const id = document._id.replace(/^drafts\./, "");
const params = {
draft: `drafts.${id}`,
published: id,
site: document.site,
slug,
};
const query = `!defined(*[!(_id in [$draft, $published]) && slug.current == $slug && site == $site][0]._id)`;
return client.fetch(query, params);
}Then in your schema:
{
name: 'slug',
type: 'slug',
options: {
source: 'title',
isUnique: isPageSlugUnique
}
}Key changes from v2 to v3:
- Remove React hooks - No more
useClient(),useState(), oruseEffect() - Use context parameter - The second parameter contains
getClient,document, and other utilities - Return a promise directly - Just return the
client.fetch()promise; Studio handles the async resolution - Access document from context - Get
documentfrom thecontextparameter instead ofoptions
This pattern is documented in this community thread where the Sanity team clarified that isUnique receives async (value, {client, document, getClient}) as parameters.
The reason your v2 code broke is that Studio v3 removed the parts system and hooks can only be used in React components, not in schema configuration functions. The context parameter is the new way to access Studio utilities in schema functions like isUnique, initialValue, and others!
Show original thread2 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.