Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

How to correctly model an array type with an image and url

1 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

Sanity– build remarkable experiences at scale

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

Was this answer helpful?