Setting readOnly and hidden fields with initialValue in Sanity schema

24 replies
Last updated: Jun 30, 2021
text: {
    type: 'string',
    name: 'type',
    initialValue: 'text',
    readOnly: true,
    hidden: true,
  },
AI Update

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 field
  • name: 'type' - The field identifier/key in your document
  • initialValue: 'text' - Sets the default value to 'text' when a new document is created
  • readOnly: 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 thread
24 replies
Which version of the studio are you running? Version
v2.12.0
and
v2.12.1
had a bug where this would happen, but it was fixed in
v2.12.2

(Appreciate if you use a single message to ask questions by the way, so it's a little easier to reply in a thread
🙂)
let me check
➜  cms git:(feature/form-builder-cms) ✗ sanity upgrade

✔ Saved lockfile

✔ Modules upgraded:
@sanity/cli        2.12.0 → 2.12.2
@sanity/core       2.12.0 → 2.12.2
@sanity/desk-tool  2.12.0 → 2.12.2
Let me know if that solved the issue 🙂
testin right now
nope
the fields is still there
how can I get the version of the studio?
sanity versions
in the studio root
I did it
but there’s any studio
Did you restart the sanity development server after upgrading? Also, could you paste the entire schema type definition? Fields should be declared in an array, but the example you showed is an object, so I'm a bit confused
yep
yeah cause I spread that fields
I have this
export default [
  {
    title: 'Layout',
    name: 'layout',
    type: 'string',
    fieldset: 'layout',
    initialValue: 'wide',
    required: true,
    options: {
      list: [
        { title: 'Largo', value: 'wide' },
        { title: 'Stretto', value: 'narrow' },
      ],
    },
  },
  {
    type: 'boolean',
    name: 'required',
    title: 'Richiesto',
    initialValue: false,
  },
];
export default [
  {
    title: 'Layout',
    name: 'layout',
    type: 'string',
    fieldset: 'layout',
    initialValue: 'wide',
    required: true,
    options: {
      list: [
        { title: 'Largo', value: 'wide' },
        { title: 'Stretto', value: 'narrow' },
      ],
    },
  },
  {
    type: 'boolean',
    name: 'required',
    title: 'Richiesto',
    initialValue: false,
  },
];
and I’m using it in this way
fields: [

...defaultForm.fields,

typeOfFields.text,

{

type: 'string',

name: 'label',

title: 'Label del campo',

initialValue: 'Cognome',

},

],
where the code above is spread by
...defaultForm.fields
But I’ve tried to put them without spread and the result is the same
But I’ve tried to put them without spread and the result is the same
fixed after a couple restart 🙂
thank for your time

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.

Was this answer helpful?