Setting readOnly and hidden fields with initialValue in Sanity schema
Looking at your code snippet, it appears you're defining a schema field with several properties. Let me explain what each property does:
text: {
type: 'string',
name: 'type',
initialValue: 'text',
readOnly: true,
hidden: true,
}Here's what each property means:
type: 'string'- Defines this as a string fieldname: 'type'- The field identifier/key in your documentinitialValue: 'text'- Sets the default value to 'text' when a new document is createdreadOnly: true- Makes the field uneditable in the Studio UI (users can see it but can't change it)hidden: true- Hides the field completely from the Studio UI
This pattern is commonly used for fields that need to store metadata or type discriminators but shouldn't be edited by content creators. Since both readOnly and hidden are set to true, the hidden property takes precedence and the field won't be visible at all in the Studio.
Note: According to the Conditional Fields documentation, both hidden and readOnly can also accept callback functions if you need dynamic behavior:
hidden: ({document, parent, value}) => {
// Return true to hide, false to show
return someCondition;
}If you're using this field as a type discriminator (which the name suggests), this is a solid approach for maintaining data structure without exposing implementation details to content editors. The initialValue ensures new documents automatically get the correct type value.
Show original thread24 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.