How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

How to preserve validation on required relational fields after removing the relation

4 repliesLast updated: Dec 1, 2025

So I noticed that when you link a relation as a required field and remove the relation, the validation is still valid
AI Update

I understand you're seeing an issue where a required reference field still validates as valid after you remove the relation. Let me help clarify what's happening here.

What's Actually Occurring

When you "remove" a reference in Sanity Studio, you might be seeing one of these scenarios:

The Expected Behavior

According to the reference type documentation, a valid reference must have both:

The built-in required() validation should prevent publishing when a required reference field is empty or missing.

Troubleshooting Steps

First, let's verify what's actually in your document when you "remove" the reference:

{
  name: 'myReference',
  type: 'reference',
  to: [{type: 'targetType'}],
  validation: (rule) => rule.required()
}

If the Issue Persists

If you're genuinely seeing validation pass with a missing required reference, you can add explicit custom validation as a workaround:

{
  name: 'myReference',
  type: 'reference',
  to: [{type: 'targetType'}],
  validation: (rule) => rule.required().custom((value) => {
    // Explicitly check for a valid reference
    if (!value?._ref) {
      return 'This reference is required'
    }
    return true
  })
}

This custom validation explicitly checks that the _ref property exists, which contains the actual document ID.

Important Context

Keep in mind that validation in Sanity Studio is client-side only. This means:

If you're consistently able to reproduce this issue with specific steps, I'd recommend reporting it on the Sanity GitHub repository with details about your schema configuration and the exact steps to reproduce, as this would help the team investigate whether there's a genuine bug in the validation system.

Show original thread
4 replies

Was this answer helpful?

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.

Related contributions