Best route for year entry in a document and handling height as input.
For a year input field in Sanity, you have a couple of solid options depending on your needs:
Option 1: Number field with validation (Recommended for most cases)
Use a number field with validation rules to constrain the input to valid years:
defineField({
name: 'year',
title: 'Year',
type: 'number',
validation: (Rule) => Rule.required().min(1900).max(2100).integer()
})This gives you:
- Clean numeric data storage
- Built-in validation to prevent invalid entries
- Simple input field that's familiar to users
- The
.integer()rule ensures no decimal values
You can adjust the min and max values to suit your needs. For example, if you only need recent years, you could set min(2000) and max(new Date().getFullYear() + 10).
Option 2: String field with a predefined list (dropdown)
If you want a dropdown with specific years, use a string field with an options.list:
defineField({
name: 'year',
title: 'Year',
type: 'string',
options: {
list: [
{ title: '2025', value: '2025' },
{ title: '2024', value: '2024' },
{ title: '2023', value: '2023' },
// ... more years
]
}
})The downside is you need to manually maintain the list. You could generate it programmatically in your schema:
const currentYear = new Date().getFullYear();
const years = Array.from({ length: 50 }, (_, i) => {
const year = (currentYear - i).toString();
return { title: year, value: year };
});
defineField({
name: 'year',
title: 'Year',
type: 'string',
options: { list: years }
})Option 3: Date field (if you need full dates)
If you might eventually need more than just the year, you could use a date or datetime field and format it to show only the year in your frontend. However, this requires users to select a full date (like January 1st of that year), which might be awkward if you truly only care about the year.
My recommendation: Go with Option 1 (number field with validation) for the best balance of simplicity, data integrity, and user experience. It prevents invalid entries without requiring you to maintain a list of years that grows every January!
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.