Setting default values for video options in a document with Sanity.io
Yes, you can definitely set initial values for an array of objects like that! Sanity supports the initialValue property on fields, and it works with arrays of objects too.
For your specific schema, you can add initialValue at the array level. Here's how:
{
title: "Media",
name: "media",
type: "array",
initialValue: [
{
_type: "image",
// You can't really pre-populate image assets, but the type is set
},
{
_type: "object",
_key: "video-default", // Arrays need unique _key values
url: "https://example.com/default-video.mp4",
autoplay: false,
controls: true,
},
],
of: [
{
title: "Image",
type: "image",
},
{
title: "Video",
type: "object",
fields: [
{
title: "Video Url",
name: "url",
type: "url",
},
{
title: "Autoplay",
name: "autoplay",
type: "boolean",
initialValue: false, // Field-level default
},
{
title: "Controls",
name: "controls",
type: "boolean",
initialValue: true, // Field-level default
},
],
},
],
}A few important notes:
Field-level vs array-level: You can set
initialValueon individual fields (likeautoplayandcontrolsabove) which will apply whenever someone adds a new video object to the array. This is often more practical than pre-populating the entire array.Array items need
_type: When setting initial values for arrays with multiple types, each item needs a_typeproperty matching one of the types in yourofarray.Dynamic initial values: If you need more complex logic,
initialValuecan be a function:
initialValue: () => [{
_type: "object",
autoplay: false,
controls: true,
url: ""
}]- Initial Value Templates: For more sophisticated scenarios (like setting values based on user context or parameters), you might want to use Initial Value Templates configured in your
sanity.config.tsfile.
The field-level approach (setting initialValue on autoplay and controls) is usually the most maintainable solution for your use case, as it ensures every new video object gets sensible defaults without pre-populating the entire array.
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.