Disable Sanity Studio validation errors to fix incorrect API data

2 replies
Last updated: May 14, 2020
hey everyone! how are you doing?

Is there any way to disable the error validations preventing Publish? We’ve pushed some incorrect data via API (which it accepted), and we’re trying to fix it using the Studio but sadly since it’s erroring out (“Value did not match…“), we can’t fix it easily that way
AI Update

I completely understand your frustration! Unfortunately, there's no built-in way to temporarily disable validation rules in Sanity Studio to allow publishing of documents that don't meet validation requirements. This is a common pain point when data gets imported via the API (which bypasses client-side validation) and then needs to be fixed in the Studio.

Here are your best options to fix this situation:

The most straightforward approach is to temporarily comment out or remove the validation rules from your schema definition:

defineField({
  name: 'yourField',
  type: 'string',
  // validation: (rule) => rule.required(), // temporarily disabled
})

After making this change:

  1. Restart your Studio dev server
  2. Fix the problematic data in the Studio
  3. Publish the corrected documents
  4. Re-enable the validation rules
  5. Restart again to enforce validation going forward

This is the cleanest approach because it lets you use the Studio's UI to fix the data properly.

Option 2: Fix via API or Migration Script

Since the API bypasses Studio validation, you can write a script to fix the data programmatically using the Mutation API. This is especially useful if you have many documents to fix:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  token: 'your-token',
  apiVersion: '2024-01-01'
})

// Query for problematic documents and patch them
const documents = await client.fetch(`*[_type == "yourType"]`)

for (const doc of documents) {
  await client
    .patch(doc._id)
    .set({ yourField: 'corrected-value' })
    .commit()
}

This approach is particularly useful for bulk fixes or when the correction logic is straightforward enough to script.

Important Context About Validation

As noted in the field validation documentation, validation in Sanity is client-side only within the Studio. This is why the API accepted your incorrect data in the first place – the Mutation API doesn't enforce the same validation rules that the Studio does. This design gives you flexibility but means you need to implement your own validation layer if you're accepting data via API.

For future imports, consider validating your data against your schema rules before pushing to the API, or implement a validation layer in your import scripts to catch these issues before they reach your dataset.

The temporary schema modification approach (Option 1) is usually the fastest way to unblock yourself and get your content back to a publishable state!

Show original thread
2 replies

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?