Setting default values for video options in a document with Sanity.io

13 replies
Last updated: May 28, 2020
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.

I need to put default values on my video options (autoplay, controls, audio....)
(The whole thing is a document)
I need to put default values on my video options (autoplay, controls, audio....)
(The whole thing is a document)
For controls, for example, this should work:
...
initialValue: {
  media: [{
    _type: 'video',
    controls: true
  }]
},
...
Thanks
user M
user M
It works with
_type: "object"
But can I reference it from an object somewhere else with type: “video”. My document is a total mess now 😅
Depends on the schema - you might have to nest a lever deeper with
_type: 'video'
inside a
_type: 'object'
in that case I suspect. Or slightly differently. Would be good to see the schema 🙂
Sure, here is the schema
Basically I had everything in an object “Gallery”, which is cleaner, but then I needed initial values
Seems to work perfectly with
_type: video
and then my video object:

export default {
  title: "Video",
  name: "video",
  type: "object",
  fields: [
    {
      title: "Video Url",
      name: "url",
      type: "url",
    },
    {
      title: "Autoplay",
      name: "autoplay",
      type: "boolean",
    },
    {
      title: "Controls",
      name: "controls",
      type: "boolean",
    },
    {
      title: "Loop",
      name: "loop",
      type: "boolean",
    },
    {
      title: "Audio",
      name: "audio",
      type: "boolean",
    },
  ],
}
in case someone is interested, I passed the initial value to “gallery” like this
  initialValue: {
    gallery: {
      _type: "object",
      media: [
        {
          _type: "video",
          autoplay: true,
          controls: true,
          loop: true,
          audio: true,
        },
      ],
    },
  },

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?