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

Discussion on using document level validation, history API, and custom hooks to validate and set updatedAt and publishedAt fields in Sanity.io.

10 replies
Last updated: Nov 22, 2022
Is it possible to have a validation for my
updatedAt
to be greater than the published date? Basically I don't want someone to be able to put that the article was updated at an older date than when it was published... I honestly wish this stuff was baked into Sanity 😐 but alas I have to manually do my own publishedAt and updatedAt fields.
Nov 20, 2022, 8:14 PM
It is baked into document level validation , where you can set, that a value cannot be bigger/smaller then the sibling or any other field in the whole document, or even the studio or the whole internet if you setup an async validation πŸ˜‰The other question is why you don’t use the
history api for this and safe the doc history in some hidden fields, if you need longer retentions than a year?Another option is a (or even multiple) custom hook in the studio that automatically set those times and the person who set it through
currentUser()
from the Presence API whenever someone does changes the doc/a certain field etc.!This way you dont have any gaps in the data and dont have to make your editors do this
πŸ™‚
Nov 22, 2022, 12:17 PM
Happy to give you more pointers, if you tell me if you want it, and what you need
Nov 22, 2022, 12:18 PM
Well I didn't know about the history API or the Presence API πŸ˜… and what they could potentially be used for. I know about the document level validation, I just didn't know how to grab another field's value and compare
Nov 22, 2022, 3:08 PM
Looking at the history API, I would only be able to get 3 days which I think would be fine? So for this example (screenshot), I would just manually set the published at date like I currently am and then grab that last published of 14 hours? πŸ€”
Nov 22, 2022, 3:18 PM
the thing you are looking at is just the visualisation of the history api, not the api itself πŸ™‚
Nov 22, 2022, 5:06 PM
More is in our docs and refs πŸ˜‰
Nov 22, 2022, 5:07 PM
Yeah I was just trying to visualize what I would be grabbing πŸ˜…
Nov 22, 2022, 5:08 PM
Its pretty neat with both, you could even display both in a custom badge
Nov 22, 2022, 5:08 PM
Gotcha, ok just gotta figure out how to call the history API as the docs just talk about the history, not how to leverage it
Nov 22, 2022, 5:10 PM
Nov 22, 2022, 5:13 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?