
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeGreat question! Your code is working perfectly, and you've actually discovered one of Sanity's most useful features - Conditional Fields. Let me break down exactly how it works:
{
name: "flag",
title: "Show or Hide",
type: "boolean",
},
{
name: "entry",
type: "string",
title: "Entry",
hidden: ({document}) => !document.flag
}The hidden property accepts a callback function that determines whether the field should be hidden. This function:
{document} - this gives you access to the entire document being editedtrue means hide the field, false means show it!document.flag - the ! (not operator) flips the boolean value:flag is true → !document.flag is false → field is shownflag is false → !document.flag is true → field is hiddenSo your "entry" field appears when the checkbox is checked, and disappears when unchecked!
The callback function gives you access to several parameters you can use:
hidden: ({parent, document, value, currentUser}) => {
// parent - the parent object (useful in nested fields)
// document - the entire document
// value - the current field's value
// currentUser - info about who's editing
}Example with multiple conditions:
{
name: "specialContent",
type: "text",
hidden: ({document}) => document.contentType !== 'premium' || !document.isPublished
}Example using parent (for fields inside objects):
{
name: 'videoUrl',
type: 'url',
hidden: ({parent}) => parent?.mediaType !== 'video'
}If you ever need a field to be required only when visible, combine conditional fields with conditional validation:
{
name: "entry",
type: "string",
hidden: ({document}) => !document.flag,
validation: (Rule) => Rule.custom((value, context) => {
if (context.document?.flag && !value) {
return 'Entry is required when flag is checked'
}
return true
})
}Your solution is actually the recommended approach - clean, simple, and exactly how Sanity intended conditional fields to work. Welcome to Sanity, and happy building! 🎉
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