Toggle input initialValue not setting to true in Sanity schema
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:
- Manually in Studio: Just go through and check the box for existing documents
- 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:
- Click the "Create" button to make a completely new document
- Look at the "Show Product Reviews" toggle - it should be checked by default
- 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 thread2 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.