URL field validation error in block: "Validator for flag 'uri' not found
Based on the error you're seeing, the issue is that you're applying the validation to the wrong schema level. When working with annotations (like links) inside Portable Text blocks, the validation needs to be applied to the annotation definition itself, not to individual fields within the annotation.
The error Validator for flag "uri" not found for type "Object" indicates that you're trying to apply Rule.uri() to an object type field, but this validator only works on string-type fields.
Here's how to properly structure your annotation with URL validation:
{
name: 'pageContent',
type: 'array',
of: [
{
type: 'block',
marks: {
annotations: [
{
name: 'link',
type: 'object',
fields: [
{
name: 'href',
type: 'url', // This should be type 'url', not 'string'
validation: Rule => Rule.uri({
scheme: ['http', 'https', 'mailto', 'tel']
})
}
]
}
]
}
}
]
}The key points:
- Use
type: 'url'for the href field - TheRule.uri()validator works with theurlfield type - Apply validation to the href field - Not to the annotation object itself
- The scheme array allows you to specify which URI schemes are valid
If you're still having issues, make sure you're not accidentally applying the validation at the wrong level in your schema. The validation should be on the href field (or whatever you've named your URL field) within the annotation definition.
Also note that when you use type: 'url', Sanity's built-in URL field already does basic URL validation, and the Rule.uri() with custom schemes extends that to accept mailto: and tel: links in addition to standard HTTP(S) URLs.
If you need to reference the URL field in queries or want more context about how annotations work in Portable Text, check out Sanity's guide to Portable Text which explains the structure of blocks, marks, and annotations in detail.
Show original thread6 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.