How to manually trigger Sanity validator for custom input component?

32 replies
Last updated: Dec 19, 2022
Hi ! I have created a custom Input that has a button which manipulates the value of the input.It is working but I have to press a letter in the input for Sanity to run the validator and allow me to publish the post.Otherwise, the "Publish" button stays disable even though everything is ok.
How can I manually run the Sanity validator ?
Thanks a lot !
AI Update

I understand your frustration! When you programmatically update a value in a custom input component via a button click, Sanity doesn't automatically re-run validation, which can leave the Publish button disabled even though your content is valid.

Unfortunately, there is no officially documented API to manually trigger validation in Sanity Studio's custom input components. According to the custom input components documentation, your component receives props like onChange, onFocus, and onBlur, but these are designed for updating values and managing focus stateโ€”not for explicitly triggering validation.

The validation system in Sanity Studio is primarily triggered by user interactions with form fields, and the internal validation mechanism isn't exposed as a callable method to custom components.

What You Can Try (Undocumented Workarounds)

While these aren't officially documented solutions, developers in the community have found some approaches that sometimes work:

1. Try calling onBlur() after your update

Some developers report that calling onBlur() can sometimes prompt Studio to revalidate as a side effect:

const MyCustomInput = (props) => {
  const { value, onChange, onBlur } = props;

  const handleButtonClick = () => {
    const newValue = /* your transformed value */;
    
    onChange(set(newValue));
    
    // May trigger revalidation as a side effect
    if (onBlur) {
      onBlur();
    }
  };

  return (
    <div>
      {/* your input UI */}
      <button onClick={handleButtonClick}>Transform</button>
    </div>
  );
};

Important caveat: This isn't documented behavior and relies on a side effect. The onBlur prop is simply a focus management callback, not a validation trigger. It may not work consistently across Studio versions or could break in future updates.

2. Ensure you're using proper patch events

Make sure you're correctly using the PatchEvent system as described in the documentation:

import { set, unset } from 'sanity'

onChange(newValue ? set(newValue) : unset())

3. Focus/blur a hidden input element

Some developers add an invisible native input that you can programmatically focus and blur:

const inputRef = useRef(null);

const handleButtonClick = () => {
  const newValue = /* your transformed value */;
  onChange(set(newValue));
  
  // Attempt to trigger validation through focus cycle
  inputRef.current?.focus();
  inputRef.current?.blur();
};

return (
  <div>
    <input 
      ref={inputRef} 
      style={{ opacity: 0, position: 'absolute', pointerEvents: 'none' }} 
      tabIndex={-1}
    />
    {/* your actual UI */}
  </div>
);

Again, this is a workaround that exploits how Studio's validation might be triggered internallyโ€”not an official API.

Better Approach: Rethink Your UX

If these workarounds feel fragile (because they are), consider whether you can adjust your component's design:

  • Could the button update trigger an actual input change that the user sees?
  • Could you use a native input element that the user interacts with directly after the button click?
  • Could validation be relaxed or adjusted in your schema to better match the programmatic updates?

Report This Issue

Since this is a common pain point without a documented solution, I'd recommend:

  1. Checking the Sanity GitHub discussions to see if others have found better solutions
  2. Posting in the Sanity Slack community for real-time help
  3. Opening a feature request for an explicit revalidate() API in custom input components

The lack of a documented validation trigger API is a real limitation when building custom inputs with programmatic updates, and the Sanity team would benefit from hearing about your use case.

Show original thread
32 replies
I think the more important question is: what does the validation do at the moment?Seems like there might be something setup in a unfortunate way
๐Ÿ™‚
The more you share the faster we can help ๐Ÿ˜‰ (slick code examples help too)
Hi and thanks for the answer !Actually, I have created a custom component to generate a slug based on the page title, but then I have figured it out that I can have the same result by just adding "slugify" to options object
Thanks ๐Ÿ™‚
Wonderful! There are some neat things you can do with slugs, BUT I would not hardcode parents into the slug:1. You need the slug to work even when the parent changes (seo etc. will suffer if you dont do so)
2. your front-end should handle this, so you can change your content / data without having to change your whole frontend with it.
What kind of frontend are you using?
I will use a SPA &amp; SSR front end, using Next framework
Thank you for your suggestion. Actually I am working, and this topic is about a personal project, so, after work I will carefully read and think about my options.Thanks very much Saskia !
Especially there you do not need any slug with breadcrumps: since you have folders as subpages, use the query with filters and nextJs will do the rest for you
true point ! Thank you !
Its always better to handle those in the FE, because then you have bigger content velocity:lets display this blogpost in
<http://yourpage.com/NAMEOFTHEPOST|yourpage.com/NAMEOFTHEPOST>
and later / or also on
<http://yourpage.com/blog/NAMEOFTHEPOST|yourpage.com/blog/NAMEOFTHEPOST>
and maybe even in your app, and your XY. See what i mean? the bread crumps will lead you into a one way street, only the slug (which also has the HANDY feature of saving different versions of your slug since
slug.current
) can be mixed and matched, will have amazing for seo (verbose urls) and well safe you A LOT of time at some point.
Thank you very much Saskia !To be honest, I was so focused on Sanity and what can I offer to my clients that I ended up forgetting the "big picture" and if some feature makes/not makes sense in my particular stack.
I am a front-end developer that would like to start a side-job doing some web-sites that has sanity as BackOffice.
For that I am taking some time to add a wide variety of features to a "template BackOffice".
By the way: I have created some blocks (hero, gallery, etc) to pages and in a blog listing page.I would like to add a "listing" block so that the user could change the order of the blocks, and place some block under/above the listing.
I was thinking about giving the user the opportunity to choose the listing layout (row/grid).
Then I wonder: Is it possible to have like a UI that I manually place the blocks in ? Like elementor, in wordpress
๐Ÿ˜‚And the reason is to offer the user the possibility to have a block in a sidebar, for example.
I can always set a dropdown with layout options but...
And the reason is to offer the user the possibility to have a block in a sidebar, for example.I can always set a dropdown with layout options but...
Well, you need to rethink what you know about Wordpress and other CMSes and take the structured content :sanity_with_3_hearts: approach: Since everything is data, you can setup an array of page sections and let them mix, match, shuffle and change, whatever they want ๐Ÿ˜Ž
Ok, thanks a lot Saskia, once again ๐Ÿ‘
As an inspiration: Some of my (older) work
BTW: previews are your best friend here and will take a lit of time, but they are worth the extra step, so people can more easily find their way
yes ! It's on my list ! I like visual content, so I will definitely include them !
I have been watching some youtube videos as inspiration
For previews?
no, for the Backoffice features, in general.
I am brand new on sanity world, and on the structured content approach (as you already got)
I am a front end developer... but a curious one ahah
The beauty is that sanity is more JAM stack then full stack, so based on front-end logic basically ๐Ÿ™‚ (at least for you users)
I took an udemy online course about Sanity. A bad one, by the way... All I am learning in from your documentation which is very helpfull
Yeah same ... Learnt the most from docs etc. But react I learned from travery media :) Who gives the udemy course you did not enjoy ?
But I can really recommend our YouTube channel as well. We have a lot of amazing tutorials, meetups and idea driven things like the structured content conf
the react course I took was very good. react + next
Hey ! Could you please share with me a link that explains how to place an image next to the icon, on the preview ?I can only place one media in the preview (the block icon).
I need to do that and find a good fallback UI when the user dont insert title, for example
Thank you very much !

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?