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

Schema Specification for a Document - Is There a Way to Specify That Field To Be Unique?

6 replies
Last updated: Jul 13, 2020
In schema specification for a document, is there a way to specify that I want a field to be unique? That no two documents with the same content in that field can exist? I find myself cleaning up duplicates quite a lot...
Jul 10, 2020, 7:44 PM
If this is an array field for example, you could add the
unique()
function to the validation. This requires all values within the array to be unique by doing a deep comparison.

{
  title: 'Category Set',
  name: 'categorySet',
  type: 'array',
  of: [{type: 'string'}],
  validation: Rule => Rule.unique()
}
Jul 10, 2020, 8:08 PM
Hmm... Can the validation rule be put on other types as well, or does it have to be an array?
Given this:

  type: 'document',
  title: 'Language texts',
  name: 'i18n',
  fields: [
    {
      name: 'key',
      title: 'Identifier',
      type: 'string',
    },
    {
      name: 'value',
      title: 'Translations',
      type: 'localeString',
    },
  ]
I want the
key
field to be unique.
Jul 10, 2020, 8:11 PM
Yes it can be applied to the
key
field. Thank you very much! πŸ™‚
Jul 10, 2020, 8:18 PM
Indeed! Thanks for posting back πŸ™‚
Jul 10, 2020, 8:37 PM
Well I was a bit quick on reporting success with the validation rule here. The
unique
rule is not valid on document descriptors. I wish it was.
That leaves me with trying to create a custom rule which queries for document keys on input. Anybody have a clue as to how I can query existing objects inside a custom rule?
Jul 13, 2020, 5:42 AM

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