🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Issue with custom slug validation in Sanity Studio v3 is resolved.

2 replies
Last updated: May 8, 2024
hi everyone, I need some help on
I just migrating sanity studio to v3, and facing this issue

so I have an slug error regarding on custom validation
here's my custom slug validation code (it was fine on sanity studio v2)

import { useClient } from "sanity";

export function isPageSlugUnique(slug, options) {
  const client = useClient({ apiVersion: "2022-02-03" });
  const [result, setResult] = useState(false);

  const { document } = options;
  const id = document._id.replace(/^drafts\./, "");

  useEffect(() => {
    async function fetchDocument() {
      const params = {
        draft: `drafts.${id}`,
        published: id,
        site: document.site,

      const query = `!defined(*[!(_id in [$draft, $published]) && slug.current == $slug && site == $site][0]._id )`;
      const res = await client.fetch(query, params);

  }, []);

  return result;
and it always returning an error like this image
any advice to solve this?
May 8, 2024, 1:43 AM
It’s number 2: breaking the rules of hooks. Your function isn’t a React component and so you can’t use the useEffect and useClient hooks. You’ll want to bring in the client in another way.
May 8, 2024, 2:52 AM
ah thats correct, it solved now 🎉
May 8, 2024, 3:15 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?