Hi there!
I am using this Joi.date().min("now") for the validation of this input <input type="date" id="scheduleFrom" name="scheduleFrom">.
In order to check that value, the date is coming with the sting of that(YYYY-MM-DD) input. However, with the current date(today) I receive an error that the date should be equal or big than "now" value.
I've got similar issue
Frontend is sending 2020-04-14T00:00:00.000Z
min('now') is used to validate on Server end
Generates an error for current day, because min('now') has date-time format and always gonna be greater then the value with date format
I also have the same issue with date formatted as ("YYYY-MM-DD")
I'm trying to compare input date if it equals to today or tomorrow.
Tomorrow works fine but comparing with min(today) fails telling me that it should be greater than value.
I basically put date of today in YYYY-MM-DD format as return value for today() and also tomorrow() the same way. max(tomorrow()) is fine but min(today()) fails during comparison.
const myValidation = (data) => {
const schema = Joi.object({
topic: Joi.string().min(2).required(),
description: Joi.string().min(2).required(),
date: Joi.date()
.format("YYYY-MM-DD")
.min(today())
.message('"date" cannot be earlier than today')
.max(tomorrow())
.message('"date" cannot be later than tomorrow')
.required(),
template: Joi.boolean().optional(),
tags: Joi.array().items(Joi.string()),
});
return schema.validate(data);
};
The problem is that
Joi.date().min('min')
contains date & time, and if you trying to validate only date, it will be less than current time.
My workaround this is to add in the context a todayDate and then reference it in the min value.
import { startOfDay } from 'date-fns';
const data = {
checkin: '2020-05-14',
};
const schema = Joi.object().keys({
checkin: Joi.date().min(Joi.ref('$todayDate')).required(),
});
// assume that this validation is not in the same file :]
const result = await Joi.attempt(data, schema, { context: { todayDate: startOfDay(new Date()) } });
now was introduced to allow for "future-enough" dates to be passed, it's not meant to have millisecond (or even second or minutes) accuracy, in which case you're going to have problems no matter what (eg. client-server not being exactly synced, time between form entry and query, http latency, etc...). I don't think it's a problem with joi but rather how time is considered in your app. Time is a difficult problem and the requirements need to be thought of carefully. Even startOfDay might be a difficult concept in a multi timezone app. There's nothing actionable here IMHO.
Thanks everybody for the response.
Most helpful comment
I also have the same issue with date formatted as ("YYYY-MM-DD")
I'm trying to compare input date if it equals to today or tomorrow.
Tomorrow works fine but comparing with min(today) fails telling me that it should be greater than value.
I basically put date of today in YYYY-MM-DD format as return value for today() and also tomorrow() the same way. max(tomorrow()) is fine but min(today()) fails during comparison.