Is there any way to set a default value in a document field?

5 replies
Last updated: Sep 13, 2020
Is there any way to set a default value in a document field?
AI Update

Yes! Sanity provides two main ways to set default values in document fields:

1. Field-Level Initial Values (Simple Defaults)

For straightforward default values, you can use the initialValue property directly in your schema field definition:

defineField({
  name: 'approved',
  type: 'boolean',
  initialValue: true
})

This works great for static defaults that should apply to all new documents of that type.

2. Initial Value Templates (Dynamic Defaults)

For more complex scenarios, Sanity has a powerful Initial Value Templates feature that lets you:

  • Set dynamic values based on context
  • Create multiple creation options for the same document type
  • Access current user information
  • Fetch data from APIs or existing documents
  • Pass parameters from the creation flow

Here's an example that sets the author field to the current user:

// sanity.config.ts
export default defineConfig({
  document: {
    newDocumentOptions: (prev, context) => {
      return [
        ...prev,
        {
          id: 'article-with-author',
          title: 'New Article',
          schemaType: 'article',
          value: async (params, context) => {
            const {currentUser} = context
            return {
              author: {
                _type: 'reference',
                _ref: currentUser.id
              },
              publishedAt: new Date().toISOString()
            }
          }
        }
      ]
    }
  }
})

Initial Value Templates are especially useful for parent-child relationships, where you want to automatically set a reference to a parent document when creating a child from a specific context.

The main difference: use initialValue for simple static defaults, and Initial Value Templates when you need dynamic values, user context, or multiple creation options for the same document type.

This should be a good starting point: https://www.sanity.io/docs/initial-value-templates
Wow sorry, didin't take a look at that πŸ˜„ thanks a lot!
user U
is it possible to apply this in an object?
Not at my computer atm but it should work in theory, just give it a try and let us know.
From my understanding, this doesn't work for adding new objects to an existing document. But I believe there is a way to set an initial value for child objects when creating a new document. They way I think about it is, initial value templates are only for documents. Documents can contain objects so you can set an initial value for objects that are part of the document at creation. But not for new objects that are created later.

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?