Validation not working on image type in Sanity document
I understand your frustration! This is actually a known issue with how Sanity's validation system works with image fields. The .required() validation on image fields can be tricky because of how image data is structured.
Here's what's happening: When you add .required() to an image field, it only validates that the image field object exists, not that it actually contains an asset reference. An image field in Sanity is an object with properties like asset, crop, and hotspot. So even without an uploaded image, the field can have an empty object {}, which passes the required validation.
The solution is to use custom validation that specifically checks for the asset reference:
defineField({
name: 'image',
type: 'image',
validation: (rule) => rule.custom((value) => {
if (!value?.asset) {
return 'Image is required'
}
return true
})
})Or if you want to keep it more concise with chaining:
defineField({
name: 'image',
type: 'image',
validation: (rule) => rule.required().custom((value) => {
return value?.asset ? true : 'An image must be uploaded'
})
})The key is checking for value?.asset specifically, since that's the actual reference to the uploaded image in your Content Lake.
One important note from the validation documentation: validation only runs client-side in the Studio. If you're using API mutations directly, you'll need to implement server-side validation separately.
This same approach works for file fields too if you run into similar issues there!
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.