👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Set Default Value to a Slug - How to Set a Standard Value to a Slug

3 replies
Last updated: Mar 4, 2021
Hi! If i want to set a default value to a slug, how can i do that?
Mar 3, 2021, 9:05 AM
What you’re looking for is called “initial values”. More info here: https://www.sanity.io/guides/getting-started-with-initial-values-for-new-documents
Mar 3, 2021, 3:01 PM
Example (I’ve included slugify, to create the slug from a title string, but you could also hard code it):
const slugify = require("slugify");

const title = "Test", 
slug = slugify(title, {
  lower: true,
});

export default {
  type: "document",
  title: "Pages",
  name: "page",
  initialValue: {
    title,
    slug: { _type: "slug", current: slug },
  },
  fields: [
    {
      title: "Title",
      name: "title",
      type: "string",
      validation: (Rule) => Rule.required(),
    },
    {
      name: "slug",
      title: "Slug",
      type: "slug",
      options: {
        source: "title",
        slugify: (input) =>
          input.toLowerCase().replace(/\s+/g, "-").slice(0, 200),
      },
    },
  ],
};
Mar 3, 2021, 3:04 PM
Thank you!
Mar 4, 2021, 12:11 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?