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

4 replies
Last updated: Feb 4, 2021
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:

  1. The reference field is being set to null or undefined - In this case, the required() validation should catch it and show an error
  2. An empty reference object structure remains - This is less common but could happen in certain edge cases

The Expected Behavior

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

  • _type: 'reference'
  • _ref: '<document-id>'

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:

  1. Check the document data: Open your browser's developer console and inspect what value the field actually contains when you think it's removed. You can do this by looking at the document state in Sanity Studio.

  2. Verify your schema: Make sure your reference field is properly configured:

{
  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:

  • Validation can be bypassed through direct API mutations
  • You should implement server-side validation for production systems
  • Edge cases in the UI could potentially affect validation behavior

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
This is likely because
Rule.required
simply checks for existence. Before you click the button, the field itself is undefined, when you click add, but dont input any values, Studio inserts an empty object. The field is now defined with
guide: {}
and since an object is truthy, it passes the validation check.
The solution to this is to chain some more validation-rules or to add a
Rule.custom
if you want more granular control over what is a valid document and what isnt. πŸ™‚
Haakon is spot on 🎯 You could try something like this as a custom rule:
validation: Rule => Rule.custom(field => {
  return field.length > 0
    ? true
    : 'Required'
  }),
Yeah I got it!
thank you all !

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?