Toggle input initialValue not setting to true in Sanity schema

2 replies
Last updated: Sep 19, 2022
hey everyone! i'm having a strange issue with my schema where i'm trying to set the initial value of a toggle input to be
true
by default, but for some reason, it refuses to get set to true. this is my code + a screenshot of what the result is in sanity studio:

{
      title: 'Show Product Reviews',
      name: 'showProductReviews',
      type: 'boolean',
      group: 'shopModule',
      initialValue: true
}
was wondering if anyone had any idea on what's going on or where i went wrong?
AI Update

I can totally see why this is confusing! The good news is your schema looks correct. The issue is likely that you're looking at an existing document that was created before you added the initialValue: true to your schema.

Here's what's happening: initialValue only applies when a new document is created. It doesn't retroactively update documents that already exist in your dataset. So if you had already created this document before adding initialValue: true to your schema, the field would still be false (or undefined, which displays as unchecked).

How to Fix This

Option 1: Create a New Document (quickest way to verify) Delete the existing document (or just create a new one) and you should see the toggle is checked by default.

Option 2: Update Existing Documents If you need existing documents to have this field set to true, you'll need to update them. You can do this in a few ways:

  1. Manually in Studio: Just go through and check the box for existing documents
  2. Via API/Migration: If you have many documents, you can run a migration script:
import {getCliClient} from 'sanity/cli'

const client = getCliClient()

// Fetch all documents of your type that need updating
const documents = await client.fetch(`*[_type == "yourDocumentType" && !defined(showProductReviews)]`)

// Update them
const transaction = client.transaction()
documents.forEach(doc => {
  transaction.patch(doc._id, {set: {showProductReviews: true}})
})

await transaction.commit()

Verify Your Schema Works

To confirm your initialValue is working correctly:

  1. Click the "Create" button to make a completely new document
  2. Look at the "Show Product Reviews" toggle - it should be checked by default
  3. If it's checked on new documents, your schema is working perfectly!

According to the Initial Value Templates documentation, field-level initialValue settings like yours apply to all new documents of that type when created through the Studio. Your syntax is spot-on:

{
  title: 'Show Product Reviews',
  name: 'showProductReviews',
  type: 'boolean',
  group: 'shopModule',
  initialValue: true  // ✅ This is correct
}

The key takeaway: initialValue is about initialization, not migration. It sets the default for new documents, but doesn't change existing ones. This is actually a good thing - you wouldn't want your schema changes to unexpectedly modify existing content!

Show original thread
2 replies
Did you change the schema after the document was already created? If I remember correctly the
initialValue
will only be set on documents created after adding that setting to the schema.
Hey
user F
, I think I’ve run into a similar issue that may be the source of some trouble you’re having. Is the parent of the
showProductReviews
boolean a Product created via Sanity Connect for Shopify?
If so, it seems like `initialValue`’s are only set for documents created in the Studio itself.

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?