Join us live Sept. 27 – How Sanity and Vercel powered Morning Brew's transformation –>

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.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Give your team exactly what they need to edit and publish their content with the customizable Sanity Studio. Get real-time collaboration out of the box. Sanity.io comes with a hosted datastore for JSON documents, query languages like GROQ and GraphQL, CDNs, on-demand asset transformations, presentation agnostic rich text, plugins, and much more.

Don't compromise on developer experience. Join thousands of developers and trusted companies and power your content with Sanity.io. Free to get started, pay-as-you-go on all plans.