URL field validation error in block: "Validator for flag 'uri' not found

6 replies
Last updated: Aug 6, 2020
i get an odd error after I added validation to an url field in my schema within a `block`…
to url field was not accepting
<mailto:mail@example.com>
so I added
validation: Rule => Rule.uri({
    scheme: ['http', 'https', 'mailto', 'tel']
  })
to this field. when setting a link to a
mailto:
url it never validates and throws an Error:
Error: pageContent[_key=="59a60b5078e7"].copy[_key=="2383b6a1ee99"]: Error validating value: Validator for flag "uri" not found for type "Object"

is there something I am missing here?
AI Update

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:

  1. Use type: 'url' for the href field - The Rule.uri() validator works with the url field type
  2. Apply validation to the href field - Not to the annotation object itself
  3. 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 thread
6 replies
Could you share your code for where exactly you include the validation? Is it in inside your
url
type field?
this is the complete schema for the object I create to have “page builder” -like approach
import linkIcon from 'react-icons/lib/fa/paperclip';
import moduleSettings from '../common/moduleSettings';
import icon from '../../assets/ModuleTextCentered';
export default {
  title: 'Text zentriert',
  name: 'moduleTextCentered',
  type: 'object',
  icon,
  fields: [
    {
      title: 'Titel',
      name: 'title',
      type: 'string',
    },
    {
      title: 'Subtitel',
      name: 'subTitle',
      type: 'string',
    },
    {
      title: 'Text',
      name: 'copy',
      type: 'array',
      of: [
        {
          type: 'block',
          marks: {
            annotations: [
              {
                name: 'link',
                type: 'object',
                title: 'Link',
                fields: [
                  {
                    title: 'URL',
                    name: 'url',
                    type: 'url',
                  },
                ],
                validation: Rule =>
                  Rule.uri({
                    scheme: ['http', 'https', 'mailto', 'tel'],
                  }),
              },
              {
                name: 'internalLink',
                type: 'object',
                title: 'Internal link',
                blockEditor: {
                  icon: linkIcon,
                },
                fields: [
                  {
                    name: 'reference',
                    type: 'reference',
                    to: [
                      { type: 'page' },
                      // other types you may want to link to
                    ],
                  },
                ],
              },
            ],
          },
        },
      ],
    },
    {
      title: 'Button',
      name: 'button',
      type: 'internalLinkButton',
      description: 'optionaler Link auf eine interne Seite',
    },
    ...moduleSettings,
  ],
};
The issue here seems to be that the validation is set for the
link
object instead of the
url
, so the
uri
validator flag is not available. Have you tried moving the validation to the
url
type instead, as follows?
{
  title: 'URL',
  name: 'url',
  type: 'url',
  validation: Rule =>
    Rule.uri({
      scheme: ['http', 'https', 'mailto', 'tel'],
    }),
},
doh
thanks… that solved it
It’s really easy to miss in nested structures 🙂 glad it’s working!

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?