Setting initial values for array items with image and video object types

13 replies
Last updated: Nov 29, 2025
Is it possible to get initial values from an object like this:
{
  title: "Media",
  name: "media",
  type: "array",
  of: [
    {
      title: "Image",
      type: "image",
    },
    {
      title: "Video",
      type: "object",
      fields: [
        {
          title: "Video Url",
          name: "url",
          type: "url",
        },
        {
          title: "Autoplay",
          name: "autoplay",
          type: "boolean",
        },
        {
          title: "Controls",
          name: "controls",
         type: "boolean",   
        },
      ],
    },
  ],
},

AI Update

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:

  1. Field-level vs array-level: You can set initialValue on individual fields (like autoplay and controls above) which will apply whenever someone adds a new video object to the array. This is often more practical than pre-populating the entire array.

  2. Array items need _type: When setting initial values for arrays with multiple types, each item needs a _type property matching one of the types in your of array.

  3. Dynamic initial values: If you need more complex logic, initialValue can be a function:

initialValue: () => [{
  _type: "object",
  autoplay: false,
  controls: true,
  url: ""
}]
  1. 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.ts file.

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.

Show original thread
13 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.

Was this answer helpful?