How to Make an Array Type whose Elements are an Image or URL Type

2 replies
Last updated: Feb 4, 2021
I have a trouble. I'd like to make an array type whose elements are an image type or a url type. So I coded like as below in my document schema's fields.
{
  name: 'referenceList',
  type: 'array',
  title: 'references',
  of: [{
    type: 'url'
  }, {
    type: 'image'
  }]
}
It occurs an error when I try to add a url type element (not for an image type).
What should I do to fix it?
Feb 4, 2021, 6:22 AM
Hey there, looks like you're running into the second limitation mentioned on this page: https://www.sanity.io/docs/array-type#two-limitations-fNBIr84P .
If you could wrap the URL type in an object, that should get rid of the error you're seeing. For example:

{
  name: 'referenceList',
  title: 'references',
  type: 'array',
  of: [
    {
      type: 'image'
    },
    {
      title: 'URL',
      name: 'urlObject',
      type: 'object',
      fields: [
        {
          title: 'URL',
          name: 'urlField',
          type: 'url'
        }
      ]
    }
  ]
}
Feb 4, 2021, 9:47 AM
OMG! Thanks a lot!
Feb 4, 2021, 9:51 AM

Sanity.io – build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Categorized in