Watch a live product demo πŸ‘€ See how Sanity powers richer commerce experiences

Is there a way to resolve a reference in a custom validation?

10 replies
Last updated: Oct 14, 2020
Is there a way to resolve a reference in a custom validation?I have an array that can hold two different types of references. I would like to validate that it holds at least on of each type.

Thank you in advanced

  name: 'tags',
  title: 'Tags',
  type: 'array',
  of: [
      type: 'reference',
      to: [{ type: 'artist' }, { type: 'category' }],
  validation: (Rule) =>
    Rule.required().custom((data) => {
        That is what i get from the data item. 
        But I need to know the type of the reference (artist or category)
        data [{…}]
          _key: "b29ee1dbb22a"
          _ref: "96e38ca0-b075-4777-b8f4-d2d6ca123df0"
          _type: "reference"

Sep 8, 2020, 9:49 AM
Sorry this was lost in the flow here... here's a suggestion:

import { uniq } from 'lodash'


      validation: Rule => Rule.custom(data => {
        const types = => item._type)
        const uniqTypes = uniq(types)
        return uniqTypes.length > 1
          ? true
          : 'Must have at least one of each'
Sep 20, 2020, 9:34 AM
ah.. right you need the reference...
Sep 20, 2020, 9:36 AM
Yes! Thanks for getting back. I am referring to two different document types
[{ type: 'artist' }, { type: 'category' }]
. I want to validate that at least one of each category is in the array. But in the validation, i can not resolve toe reference. Is this even possible?
Sep 20, 2020, 9:38 AM
Something along these lines... (untested)

import { uniq } from 'lodash'
import client from 'part:@sanity/base/client'


      validation: Rule => Rule.custom(data => {
        const referencedIds = => item._ref)
        return client.fetch(`*[_id in $ids]._type`, {
          ids: referencedIds
        .then(res => {
          const uniqTypes = uniq(res)
          return uniqTypes.length > 1
            ? true
            : 'Must have at least one of each'  
Sep 20, 2020, 9:42 AM
Nice! Does the client work out of the box, or do i need to add the credential somewhere?
Sep 20, 2020, 9:43 AM
It's preconfigured
Sep 20, 2020, 9:43 AM
Thanks a lot!
Sep 20, 2020, 9:43 AM
You might have to tweak it, it's just from the top of my head πŸ˜„
Sep 20, 2020, 9:44 AM
Sure, but knowing that i can use the client in the schema definitions opens a whole new universe for me 😎
Sep 20, 2020, 9:45 AM
joined the slack channel to find a solution to this same problem, very happy to have found it
Oct 14, 2020, 7:53 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the 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.

Categorized in

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing