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

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.
Jan 14, 2021, 4:57 PM
first off, this seems to do nothing:
{
      title: 'Slug',
      name: 'slug',
      validation: Rule => Rule.required(),
      type: 'slug',
      options: {
        source: 'title',
        slugify: input => input.toLowerCase()
      }
    }
Jan 14, 2021, 4:57 PM
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.
Jan 14, 2021, 5:08 PM
ah, does this only control what the “generate” button generates? I thought this would auto-format whatever you put in the input field.
Jan 14, 2021, 5:12 PM
Yes, that's correct. It won't auto-update (my guess is to protect against inadvertently breaking links).
Jan 14, 2021, 5:15 PM
that makes a lot more sense. Is that also true for
isUnique
?
Jan 14, 2021, 5:16 PM
Sorry, what do you mean by is that also true?
Jan 14, 2021, 5:17 PM
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
Jan 14, 2021, 5:18 PM
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?
Jan 14, 2021, 5:20 PM
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).
Jan 14, 2021, 5:24 PM
Oh, I see. Even better then!
Jan 14, 2021, 5:26 PM
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.
Jan 14, 2021, 5:26 PM

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?