Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

How to allow unique values in an array in Sanity.io schema.

6 replies
Last updated: Sep 21, 2023
how to allow this only one time for each,

export default defineType({

title: 'Component Builder',

name: 'pageBuilder',

type: 'array',

of: [

{

type: 'header',

validation: Rule => Rule.unique()

},

{

type: 'footer',

validation: Rule => Rule.unique()

},

],

})
Sep 21, 2023, 4:33 PM
You can create a custom validation on the array
https://www.sanity.io/docs/validation
Sep 21, 2023, 4:45 PM
already have
validation: Rule => Rule.unique()
but isnt working
Sep 21, 2023, 6:01 PM
done, thank you!
Sep 21, 2023, 6:31 PM
why don’t just add the header and the footer in separate fields?…. and the other components in the array?
Sep 21, 2023, 8:05 PM
^ or even better, create some setting documents containing all the header/footer info. Fetch it once and use it through the entire app. No need to rerender the header and footer on every route change (assuming you use some kind of frontend framework)
Sep 21, 2023, 8:08 PM

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?