How to create a field with predefined options in Sanity schema?
You're looking for a field type that provides a dropdown or selection list - in Sanity, this is done using a string field with an options property. Here's how to set it up:
defineField({
name: 'vehicleType',
title: 'Vehicle Type',
type: 'string',
options: {
list: [
{title: 'Car', value: 'car'},
{title: 'Bicycle', value: 'bicycle'},
{title: 'Truck', value: 'truck'}
]
}
})This creates a dropdown menu in the Studio where users can select one of the three options. The title is what displays in the UI, and the value is what gets stored in your content.
If you want a simpler syntax and don't need different display titles, you can use the shorthand:
defineField({
name: 'vehicleType',
title: 'Vehicle Type',
type: 'string',
options: {
list: ['car', 'bicycle', 'truck']
}
})Additional options you might find useful:
Layout: Change how options are displayed:
options: { list: [...], layout: 'radio' // Shows radio buttons instead of dropdown }Direction: For radio button layout, control orientation:
options: { list: [...], layout: 'radio', direction: 'horizontal' // or 'vertical' }
The string field with options is perfect for when you need users to select from a predefined set of values. The data is stored as a simple string value, making it easy to query and filter in GROQ.
For more details, check out the String schema type documentation.
Show original thread1 reply
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.