👀 See Sanity in action: Watch product demo now →

One or the other validation

By Roboto Studio

This validation can be used if you have two fields and you require one or the other to be filled

One or the other validation

export default {
  name: "someDocument",
  title: "Some document",
  type: "document",
  fields: [
    {
      name: "videoUrl",
      title: "Video url",
      type: "string",
      validation: (Rule) =>
        Rule.custom((field, context) =>
          context.document.image === undefined && field === undefined
            ? "Either Video url or Image must have a value"
            : true
        ),
    },
    {
      name: "image",
      title: "Image",
      type: "image",
      validation: (Rule) =>
        Rule.custom((field, context) =>
          context.document.videoUrl === undefined && field === undefined
            ? "Either Video url or Image must have a value"
            : true
        ),
    },
  ],
};

This regularly appears within websites where you can have either a video or an image for the header of the page. This allows you to validate to ensure you have one or the other.

Contributor

Other schemas by author

Easy peasy URL Slug

Studio v2

Quickly and easily validate your slug without having to work out the regex syntax

Roboto Studio
Go to Easy peasy URL Slug