๐Ÿ‘€ See Sanity in action: Watch product demo now โ†’

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.io โ€“ build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.