
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeYes, absolutely! You can create conditional validation rules that only apply when specific fields are filled out or when a boolean is checked. This is done using custom validation with the custom() method, which gives you access to the context object containing parent field values.
Here's how to implement conditional validation based on different scenarios:
defineField({
name: 'requiresApproval',
type: 'boolean',
title: 'Requires Approval'
}),
defineField({
name: 'approverEmail',
type: 'string',
title: 'Approver Email',
validation: (rule) =>
rule.custom((value, context) => {
const requiresApproval = context?.parent?.requiresApproval;
if (requiresApproval && !value) {
return 'Approver email is required when approval is needed';
}
return true;
}),
})defineField({
name: 'customOption',
type: 'string',
title: 'Custom Option'
}),
defineField({
name: 'customDetails',
type: 'text',
title: 'Custom Details',
validation: (rule) =>
rule.custom((value, context) => {
const customOption = context?.parent?.customOption;
if (customOption && !value) {
return 'Please provide details for your custom option';
}
return true;
}),
})You can also check for specific values, not just whether a field is filled:
defineField({
name: 'eventType',
type: 'string',
options: {
list: ['online', 'in-person', 'hybrid']
}
}),
defineField({
name: 'venue',
type: 'string',
validation: (rule) =>
rule.custom((value, context) => {
const eventType = context?.parent?.eventType;
if ((eventType === 'in-person' || eventType === 'hybrid') && !value) {
return 'Venue is required for in-person and hybrid events';
}
return true;
}),
})You can combine multiple conditions:
validation: (rule) =>
rule.custom((value, context) => {
const { isPublished, publishDate, category } = context?.parent || {};
if (isPublished && !publishDate) {
return 'Publish date is required for published content';
}
if (category === 'premium' && !value) {
return 'This field is required for premium content';
}
return true;
})The context?.parent object gives you access to all sibling fields within the same object. Always use optional chaining (?.) to safely access these values, and remember that validation is client-side only in Studio, so you'll need separate server-side validation if you're accepting mutations through the API.
You can also use different validation levels - return an error string to block publishing, or use .warning() or .info() for less critical feedback to editors. More details on context-dependent validation can be found in Sanity's custom validation documentation.
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