Conditional selection of image and returning a reference if the _type is image, otherwise returning the rest of the available fields.

3 replies
Last updated: May 14, 2024
Hello,I'm looking to conditionally select image and return a reference but if the _type is not image, I just want it to return the rest of the available fields. I'm not sure if that is possible

content[]{
  _type == 'image' => {
    altText,
	caption,
	"image": asset->url
  },
  // Return the rest of the fields without condition
},

May 14, 2024, 3:09 PM
Can you try:
content[]{
  ...,
  _type == 'image' => {
    altText,
	caption,
	"image": asset->url
  },
},
May 14, 2024, 3:59 PM
It worked! Thank you!
May 14, 2024, 6:40 PM
Great!
May 14, 2024, 6:43 PM

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?