Understanding slugify and isUnique in Sanity.io

11 replies
Last updated: Jan 14, 2021
I’m struggling getting slugify and isUnique to work. Silver lining: I think this is the last roadblock I’ll hit for getting started with Sanity.
first off, this seems to do nothing:
{
      title: 'Slug',
      name: 'slug',
      validation: Rule => Rule.required(),
      type: 'slug',
      options: {
        source: 'title',
        slugify: input => input.toLowerCase()
      }
    }
That looks like it should work, as long as you have a field named
title
. I think you can get rid of that slugify line as Sanity is already doing that.
ah, does this only control what the “generate” button generates? I thought this would auto-format whatever you put in the input field.
Yes, that's correct. It won't auto-update (my guess is to protect against inadvertently breaking links).
that makes a lot more sense. Is that also true for
isUnique
?
Sorry, what do you mean by is that also true?
By default, slugs are checked against other slugs of that document type. Personally, I prefer to ensure all slugs are unique using https://www.sanity.io/docs/slug-type#custom-isunique-function-d5066a58b95a
Sorry, I mean does
isUnique
work as validation on the input field for the slug or does it only impact what the generate button creates?
My understanding is that you'll never be able to create duplicate slugs on the same document type. If you provide a custom function to isUnique, you can have it do more (i.e., check against all documents). In other words, slugs by default validate (but only against the same document type).
Oh, I see. Even better then!
I don’t think I need checking against all documents so the defaults should be enough for me. I didn’t realize that came baked in.

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.

Was this answer helpful?