Set Default Value to a Slug - How to Set a Standard Value to a Slug
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),
},
},
],
};Was this answer helpful?
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.