Mailto scheme not working in Sanity URI validation

4 replies
Last updated: Apr 25, 2024
Hi there đź‘‹Has anyone encountered a similar problem? It seems that validation for mailto is not working correctly


 validation: (Rule) => Rule.required().uri({scheme: ['http', 'https', 'tel', 'mailto']}),
AI Update

I can see why you're experiencing issues with mailto: validation! This is actually a known quirk with how Sanity's URL field type works in the browser.

The good news is your validation rule is correctly configured:

validation: (Rule) => Rule.required().uri({scheme: ['http', 'https', 'tel', 'mailto']})

What's happening:

When you use type: 'url' in Sanity, it renders as an HTML <input type="url"> element. The browser's native HTML5 validation for URL input fields doesn't recognize mailto: or tel: as valid URL formats, even though they're perfectly valid URI schemes. This creates a conflict where Sanity's validation passes, but the browser blocks the form submission with its own validation error.

The solution:

Change your field type from url to string while keeping your validation rule exactly as is:

{
  name: 'link',
  title: 'Link',
  type: 'string', // Changed from 'url'
  validation: (Rule) => Rule.required().uri({
    scheme: ['http', 'https', 'tel', 'mailto']
  })
}

This approach:

  • Bypasses the browser's native URL validation that's causing the conflict
  • Still enforces proper URI format through Sanity's Rule.uri() validation
  • Works perfectly for all URI schemes including mailto: and tel:
  • Provides the same Studio experience (just without the browser's overly-strict validation)

The url field type is essentially a string field with some UI conveniences and browser-level validation. By using string with Rule.uri(), you get all the validation you need without the browser interference. The field will still validate that your users enter properly formatted URIs with the schemes you've specified - it just won't fight with the browser about what constitutes a "valid URL."

Show original thread
4 replies
I can’t seem to reproduce the error. What error text do you see when you hover over the red
?
in the circle? Can you please copy and paste the exact URL string you’re using (starting with
mailto:?body...
above)? If you take out the unescaped
?
after
care
, does the error persist?
I assume this is schema type of
url
, but had better confirm.
user A
Thanks for your time)
Turns out I was just editing the wrong schemа
:melting_face:But it took me a long time to figure it out
🥲
Ah! Glad you got it figured out! 🎉

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?