How to set a boolean field to true by default in Sanity Studio.

6 replies
Last updated: Aug 19, 2021
😟How do I make a boolean true on my sanity studio? For example, when a new document is created I want the boolean to be checked true. I used
initialValue: true
but when I created a new document it wasn't checked true by default.
AI Update

I understand the frustration! The good news is that initialValue: true is the correct approach for setting a boolean field to default to true, but there's a common gotcha that might be affecting you.

When you add or change initialValue on a field, it only applies to documents created after that change. If you're testing on an existing document (even one you just created before adding the initialValue), it won't retroactively apply the default value. You need to create a brand new document after adding the initialValue to your schema.

Here's the correct syntax for a boolean field with a default value:

defineField({
  name: 'isPublished',
  type: 'boolean',
  title: 'Published',
  initialValue: true
})

Troubleshooting steps:

  1. Make sure your Studio has reloaded after changing the schema. Sometimes you need to restart your dev server or refresh the browser.

  2. Create a completely new document (not one you started before adding initialValue). The initial value only applies to documents created after the schema change.

  3. Check for Initial Value Templates - If you have Initial Value Templates configured in your sanity.config.ts, they can override field-level initialValue settings. Templates take precedence over schema-level defaults.

  4. Verify your schema syntax - Make sure you're using initialValue (not defaultValue or another property name).

If you need more complex logic for setting initial values (like checking user context or fetching data), you can also use a function:

defineField({
  name: 'isPublished',
  type: 'boolean',
  title: 'Published',
  initialValue: () => true
})

The field-level initialValue approach should work for your use case. Just remember to test with a freshly created document after making the schema change!

Hi User. To confirm, is
initialValue: true
set at the top level of your boolean field? For example:

export default {
  name: 'booleanTest',
  title: 'Boolean Test',
  type: 'document',
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'string',
    },
    {
      name: 'someBoolean',
      title: 'Some Boolean',
      type: 'boolean',
      initialValue: true,
    },
    // ...
  ],
}
As you pointed out,
initialValue
will only trigger upon the creation of a new document (i.e., it won’t retroactively set anything).
Ahh, I see here that everything looks good. What do you see when you run
sanity versions
in your terminal? Any chance you’re on a version earlier than 2.10.0 (when
initialValue
support at the field-level was added)?
Ahh, I see here that everything looks good. What do you see when you run
sanity versions
in your terminal? Any chance you’re on a version of 2.10.0 (when
initialValue
support at the field-level was added)?
user A
Thanks for the reply, please I created a comment system and by default when someone leaves a comment I want it to be approved that is why I used the
initialValue: true
. Later when I see any spam then I uncheck it to false on my studio.Below is how the code looks like

export default {
  name: "comment",
  type: "document",
  title: "Comment",
  fields: [
    {
      name: "name",
      type: "string",
    },
    {
      title: "Approved",
      name: "approved",
      type: "boolean",
      initialValue: true,
    },
    {
      name: "email",
      type: "string",
    },
    {
      name: "comment",
      type: "text",
    },
    {
      name: "blog",
      type: "reference",
      to: [{ type: "blog" }],
    },
  ],
  preview: {
    select: {
      name: "name",
      comment: "comment",
      blog: "blog.title",
    },
    prepare({ name, comment, blog }) {
      return {
        title: `${name} commented on ${blog}`,
        subtitle: comment,
      };
    },
  },
};
user A
I just upgraded my sanity yesterday
If your documents aren't being created by a user in the studio,
initialValue
won't work; you'll need to replicate the functionality. See the first Gotcha here .

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?