👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Adding validation for featured content and sorting an array by date in Sanity schema.

3 replies
Last updated: Dec 10, 2021
Hello there, i have a schema in which a field of
boolean
contain named as isFeatured. Now i want to do validation like if isFeatured is true in any doument, i shouldn't be able to make isFeatured true in any other document and it should show which document has already isFeatured true. How to add this kinda validation. Thanks.
Dec 9, 2021, 9:48 AM
Would be better to use a reference to the featured item from the content type on which another appears as featured? For example, if you wanted to feature a blog post on the home page, the home page would have a field for FeaturedItem where the content manager could select the appropriate blog post as a reference.
This is how i approach featured content 95% of the time. It saves on maintenance because you only have to go to one place to select the new featured item and you lose the risk that people will select too many items as featured.
Dec 9, 2021, 3:09 PM
Hi
user J
Thanks for your response, another thing i want to ask is that i have an array of object and i want to order by
dateOfPublication
which is a value in objects. I am unable to write query for it.
Here's the structure,


{

"_createdAt":"2021-12-08T09:17:40Z"

"_id":"73957312-c282-4cb3-be44-1069277610dd"

"_rev":"XXZQtLc37Lz6XK4fvqH5Wt"

"_type":"newsArticleBlock"

"_updatedAt":"2021-12-09T12:51:56Z"

"newsBlockTitle":"News Cards for Home Page"

"newsCardBlock":*[*

{

"_key":"85b48badc2c2"

"_type":"newsArticle"

"articleDescription":*[*...*]*_1 item_

"articleImage":*{*...*}*

"ctaLink":*{*...*}*

"dateOfPublication":"2021-12-09T08:02:00.000Z"

"headline":"The NRP Group Breaks Ground on a Record 21 Multifamily Apartment Communities in 2020"

"isFeatured":true

"selectedCategory":*{*...*}*_2 items_

"slug":*{*...*}*_2 items_

"subHeadline":"The NRP Group subheading"

}

*{*...*}*

*{*...*}*

*{*...*}*

]

}
Dec 10, 2021, 8:09 AM
Hi Mohammad. You can sort your array using the
order()
function. There are some details here but if you want to post your schema we can probably show you where you’d add it.
Dec 10, 2021, 10:09 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?