Sanity URI validation not working with allowRelative and HTTPS scheme

7 replies
Last updated: Nov 29, 2025
Hey-O, I am not able to get the URL validation working correctly. 🤯
validation: (Rule) =>
  Rule.required().uri({
    allowRelative: true, // Allow relative links
    relativeOnly: false, // Force only relative links
    scheme: ["https"], // Default is ["https", "http"]
  }),
},
I want to be able to provide links in the input field like “/home” || “/contact”, but seems allowRelative isn’t working or that specifying the scheme to https only is blocking that rule. Any suggestions?

I’ve tried validation on local and deployed environment
AI Update

I can see the issue! When you specify scheme: ["https"] in your URI validation, it conflicts with allowRelative: true. The scheme option tells Sanity to only accept URLs with the https protocol, which means relative URLs like "/home" or "/contact" will fail validation because they don't have a scheme at all.

Here's the fix - you need to remove the scheme option when you want to allow relative URLs:

validation: (Rule) =>
  Rule.required().uri({
    allowRelative: true, // Allow relative links
    relativeOnly: false, // Allow both relative AND absolute links
    // Don't specify scheme when allowing relative URLs
  }),

If you want to only accept relative URLs (like "/home", "/contact"), use:

validation: (Rule) =>
  Rule.required().uri({
    relativeOnly: true, // Only relative links allowed
  }),

If you want to accept either relative URLs or only HTTPS absolute URLs (but not HTTP), you'll need a custom validation function:

validation: (Rule) =>
  Rule.required().custom((value) => {
    if (!value) return true;
    
    // Allow relative URLs
    if (value.startsWith('/')) return true;
    
    // For absolute URLs, only allow HTTPS
    if (value.startsWith('https://')) return true;
    
    return 'Please use a relative URL (like /home) or an HTTPS URL';
  }),

The key takeaway: the scheme option in Rule.uri() is specifically for validating the protocol of absolute URLs. When you want to allow relative URLs, either omit the scheme option entirely or use custom validation logic if you need more complex rules combining relative paths with specific scheme requirements.

Show original thread
7 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?