joi icon indicating copy to clipboard operation
joi copied to clipboard

ensure date does not occur in the past

Open ArnoldZokas opened this issue 12 years ago • 8 comments

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?

ArnoldZokas avatar Aug 26 '14 11:08 ArnoldZokas

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

arb avatar Aug 26 '14 18:08 arb

Would you accept a pull request for this?

ArnoldZokas avatar Aug 26 '14 20:08 ArnoldZokas

Absolutely.

arb avatar Aug 27 '14 13:08 arb

Sorry but no functions as inputs. I'll accept a PR that handles the string 'now' as a special case for min() and max().

hueniverse avatar Sep 11 '14 00:09 hueniverse

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 avatar Oct 23 '14 08:10 a00andos

@a00andos I am not using Joi API directly, but rather via Hapijs framework. Values passed in to min/max functions get cached.

ArnoldZokas avatar Nov 03 '14 16:11 ArnoldZokas

I think this one's been fixed in 4.8.0, closing.

Marsup avatar Jan 01 '15 16:01 Marsup

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.

lock[bot] avatar Jan 09 '20 02:01 lock[bot]