Get a peek at our latest innovations at Sanity Product Day on Dec 8th →

Validation Error: Validator for flag "uri" not found for type "Object"`

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 =&gt; 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?

Aug 6, 2020, 8:33 AM

Could you share your code for where exactly you include the validation? Is it in inside your

url
type field?

Aug 6, 2020, 9:32 AM

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 =&gt;
                  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,
  ],
};

Aug 6, 2020, 9:35 AM

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 =&gt;
    Rule.uri({
      scheme: ['http', 'https', 'mailto', 'tel'],
    }),
},

Aug 6, 2020, 10:24 AM

doh

Aug 6, 2020, 10:29 AM

thanks… that solved it

Aug 6, 2020, 10:29 AM

It’s really easy to miss in nested structures 🙂 glad it’s working!

Aug 6, 2020, 10:34 AM

Sanity.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Free to get started, and pay-as-you-go on all plans. Find out more.