Search code examples
javascriptnode.jsexpressjoi

Joi date greater than now


I am using Joi with express to validate the request. I need to validate the date field in the request body, so my code is next:

const dateSchema = Joi.date().greater(Date.now()).iso().required();

this works ok but the major problem is that Date.now() is picking up the date when I started an express server. So for example, if the server was started at 5:00, joi will not allow 4:59 but will allow everything that is greater than 5:00 even though the current time is 5:05.

How I can check Joi.date().greater(dynamicDateNow)?


Solution

  • Following joi document . You can use 'now' (string) value to compare with the current date time (when you execute validate)

    const dateSchema = Joi.date().greater('now').iso().required();