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 the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?