Issue with invisible characters in Sanity schema field value resolved

8 replies
Last updated: Jun 19, 2024
Hi. I have an issue that I cant understand.In a schema i have this field

    {
      title: 'Image Position',
      name: 'imagePosition',
      type: 'string',
      options: {
        list: [
          {title: 'Left', value: 'left'},
          {title: 'Right', value: 'right'},
        ],
      },
    },
AI Update

I can see you've shared a field definition with a string type that has an options.list property. While you mentioned you have an issue, you didn't specify what the problem is. Let me help you understand how this field works and address common issues people encounter with this pattern.

Your field definition looks correct for creating a dropdown/radio button selector in Sanity Studio:

{
  title: 'Image Position',
  name: 'imagePosition',
  type: 'string',
  options: {
    list: [
      {title: 'Left', value: 'left'},
      {title: 'Right', value: 'right'},
    ],
  },
}

This creates a string field that will render as a dropdown or radio buttons (depending on the layout) in the Studio, allowing editors to choose between "Left" and "Right".

Common issues with this pattern:

  1. Not seeing the dropdown in Studio - Make sure you've restarted your Studio dev server after adding this field. Schema changes require a restart.

  2. Getting the wrong value in queries - The stored value will be the string 'left' or 'right', not the title. Make sure your frontend code checks for these values.

  3. Want radio buttons instead of dropdown - You can add layout: 'radio' to the options:

    options: {
      list: [...],
      layout: 'radio'
    }
  4. TypeScript errors - If you're using TypeScript, wrap your field with defineField() from sanity as mentioned in the Fields documentation:

    import {defineField} from 'sanity'
    
    defineField({
      title: 'Image Position',
      name: 'imagePosition',
      type: 'string',
      options: {
        list: [
          {title: 'Left', value: 'left'},
          {title: 'Right', value: 'right'},
        ],
      },
    })
  5. Need a default value - Add initialValue: 'left' to set a default selection.

  6. Field not showing up in documents - Ensure this field is included in the fields array of your document schema type.

Could you share more details about what's not working as expected? For example: Is the field not appearing in Studio? Are you getting unexpected values when querying? Is there an error message?

The field is in the response,But when i copy the value it contains a lot of characters that is not visible.
Why is that?
Those characters makes this check to not work.
Inside the value of a field? I don't really understand.
Ah, the second link helped! Thank you!!
It's encoded data that includes a mapping to its source in the Studio. You can disable it by setting stega to false in your Sanity client or decode it by following the articles above.
Cool, glad to hear!
Really appreciate the swift response!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?