Conditional validation based on field values in Sanity
Yes, 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:
Based on a Boolean Field
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;
}),
})Based on Another Field Being Filled
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;
}),
})Based on Specific Field Values
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;
}),
})Multiple Conditional Checks
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;
})Important Notes
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.
Show original thread26 replies
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.