Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Validation error with URL field in a block in a Sanity schema

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?
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 =>
                  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 =>
    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– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?