groqd icon indicating copy to clipboard operation
groqd copied to clipboard

Cannot access "altText" inside images

Open ttavni opened this issue 1 year ago • 2 comments

I am using the sanityImage utility method and cannot access the altText which is an additional field. I'm not sure if there's be a change with sanity since the implementation but for example:

      image: sanityImage('image', {
        withAsset: ['base', 'lqip', 'dimensions'],
        additionalFields: {
          altText: q.string().nullable(),
        },
      })

It always returns null for the altText because the altText is inside asset. The only way I've got around this is

      image: sanityImage('image', {
        additionalFields: {
          asset: q('asset').deref(),
        },
        withAsset: ['base', 'lqip', 'dimensions'],
      }),

Which sort of defeats the point and ruins the types

ttavni avatar Nov 19 '23 07:11 ttavni

@ttavni Is your altText directly on the asset type and not as an extension of the image type or part of the asset metadata field?

Is it possible to post the schema of your image type?

carbonrobot avatar Nov 20 '23 14:11 carbonrobot

@carbonrobot yes that's correctly. I am using the Sanity Media plugin (https://github.com/sanity-io/sanity-plugin-media) so it is automatically adding those fields and it's directly on the asset type - nothing extra in my schema.

I didn't realise about adding custom fields on images, that makes the additionalFields make more sense now 😅 but it is nice to store the alt text for images globally instead of individually through the media plugin

ttavni avatar Nov 20 '23 19:11 ttavni