
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeGreat question! While validation rules are often project-specific, there are definitely some common patterns the Sanity community has established, especially for fields like slugs.
As the community discussion points out, most validation is heavily dependent on your team and business needs. However, certain field types like slugs have more established web standards around them (lowercase, kebab-case formatting).
For slugs specifically, check out Simeon Griggs' excellent tutorial on Next.js and Sanity slug patterns - it's widely used in production projects.
Here's a solid slug configuration with custom slugification:
defineField({
name: 'slug',
title: 'Slug',
type: 'slug',
options: {
source: 'title',
slugify: input => input
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '')
.slice(0, 200)
},
validation: (rule) => rule.required()
})defineField({
name: 'title',
title: 'Title',
type: 'string',
validation: (rule) => rule
.required()
.min(10)
.max(100)
.error('Title must be between 10 and 100 characters')
})defineField({
name: 'metaDescription',
title: 'Meta Description',
type: 'text',
validation: (rule) => rule
.max(160)
.warning('Keep under 160 characters for optimal SEO')
})defineField({
name: 'email',
type: 'string',
validation: (rule) => rule.custom((email) => {
if (!email) return true
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
return regex.test(email) ? true : 'Please enter a valid email address'
})
})defineField({
name: 'externalUrl',
type: 'url',
validation: (rule) => rule.uri({
scheme: ['http', 'https']
})
})defineField({
name: 'publishDate',
type: 'datetime',
validation: (rule) => rule.custom((value, context) => {
if (context?.document?.status === 'published' && !value) {
return 'Publish date is required for published content'
}
return true
})
})The Sanity validation system supports three severity levels:
.error()).warning()).custom())Client-side only: Validation currently only runs in the Studio. If you're using the API directly, implement server-side validation separately.
Title auto-generation: As mentioned in the defineField documentation, Sanity auto-generates titles from field names, but doesn't handle special cases well. Always explicitly set titles for acronyms (URL, SEO) and brand names (LinkedIn, GitHub).
Descriptions matter: Use the description property to explain why validation rules exist - it helps your content team understand the requirements.
The community thread also had a creative example of prepending random hex strings to slugs for guaranteed uniqueness, though that's typically unnecessary for most use cases unless you have specific requirements around collision avoidance.
What specific field types are you working with? The validation system is quite flexible and can handle pretty much any custom logic you need!
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store