ensure date does not occur in the past
I want to validate input date to ensure it does not occur in the past.
Something like this...
var schema = Joi.date().min(function() { return moment().utc().toDate(); });
Is there already a way to achieve this?
This is currently not supported but there are a few issues open related to this
- https://github.com/hapijs/joi/issues/404
- https://github.com/hapijs/joi/issues/363
Would you accept a pull request for this?
Absolutely.
Sorry but no functions as inputs. I'll accept a PR that handles the string 'now' as a special case for min() and max().
I just wrap the schema in a function for handling this:
var startWithValid = function(value) {
assert(_.isDate(value), 'must be a valid Date');
return Joi.object().keys({
start: Joi.date().min(value).required()
});
},
And then I use it like:
Joi.validate(input, startWithValid(new Date()), function (err, input) { ...
@a00andos I am not using Joi API directly, but rather via Hapijs framework. Values passed in to min/max functions get cached.
I think this one's been fixed in 4.8.0, closing.
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.