Validate updatedAt is greater than publishedAt in Sanity schema

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.
AI Update

Yes, you can absolutely validate that updatedAt is greater than publishedAt using Sanity's custom validation! Here's how to do it:

defineField({
  name: 'updatedAt',
  type: 'datetime',
  validation: (rule) =>
    rule.custom((updatedAt, context) => {
      const publishedAt = context.document?.publishedAt;
      
      if (!updatedAt || !publishedAt) {
        return true; // Allow if either field is empty
      }
      
      const updatedDate = new Date(updatedAt);
      const publishedDate = new Date(publishedAt);
      
      if (updatedDate < publishedDate) {
        return 'Updated date cannot be earlier than published date';
      }
      
      return true;
    }),
})

The key here is using Rule.custom() with the context parameter, which gives you access to context.document containing all fields in the current document. This lets you compare the two datetime fields.

You can also add the same validation to your publishedAt field to catch the issue from either direction:

defineField({
  name: 'publishedAt',
  type: 'datetime',
  validation: (rule) =>
    rule.custom((publishedAt, context) => {
      const updatedAt = context.document?.updatedAt;
      
      if (!publishedAt || !updatedAt) {
        return true;
      }
      
      if (new Date(publishedAt) > new Date(updatedAt)) {
        return 'Published date cannot be later than updated date';
      }
      
      return true;
    }),
})

As for the built-in _createdAt and _updatedAt system fields - yeah, I hear your frustration! Those are managed automatically by Sanity but they track document creation/modification in the Content Lake, not publication dates. For editorial workflows where you need to control when content appears to be "published" or "updated" from a user perspective, custom fields like yours are the standard approach. The validation system at least makes it straightforward to enforce the business logic you need.

You can find more details about custom validation in the Sanity validation documentation and this guide on adding custom validation rules.

Show original thread
10 replies
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
πŸ™‚
Happy to give you more pointers, if you tell me if you want it, and what you need
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
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? πŸ€”
the thing you are looking at is just the visualisation of the history api, not the api itself πŸ™‚
More is in our docs and refs πŸ˜‰
Yeah I was just trying to visualize what I would be grabbing πŸ˜…
Its pretty neat with both, you could even display both in a custom badge
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

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?