🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Custom Validation of an Array Based on Selection

4 replies
Last updated: Dec 9, 2021
Hey Everyone 🙂I am having some trouble with custom validation of an array. What I am trying to achieve is to change the "Rule.max()" based on the selection of a dropdown list. I am struggling to wrap my head around the controls of validation rules outside of "required". I have attached where I am currently at, which obviously doesn't work because the custom rule needs to return a bool.

If anyone could please guide me in the right direction for this custom validation that would be amazing
Dec 9, 2021, 4:22 AM
Hey Geoff! Ohh of course, so you still make it a Boolean value and just provide the context within the error message itself. Thank you so much! I was tunnel visioning on that one too much
Dec 9, 2021, 4:44 AM
You’ve got it! Rule.custom() wants to return either true or an error message. A switch might be handy if the conditions get numerous, but tomorrow I’ll take a look at your max approach as there may be an interesting way to handle it that way, too.
Dec 9, 2021, 4:45 AM
Beauty that's working nicely 🙂 The fields I am working on are nested a wee bit of a way down a doc so I switch to context.parent and it came together.
Dec 9, 2021, 4:48 AM
Ok awesome 🙂 Curious to see if you come up with something. Thanks again for your help Geoff
Dec 9, 2021, 4:48 AM

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?