Skip to content

Optional between dates with allow('') #749

@ugate

Description

@ugate

Is it possible to validate between two dates that are optional?

var schema = Joi.object().keys({
   fromDate: Joi.date().max(Joi.ref('toDate')).allow(''),
   toDate: Joi.date().min(Joi.ref('fromDate')).allow('')
});
Joi.validate({ fromDate: new Date(), toDate: '' }, schema, function (err, value) {
    if (err) console.error(err);
    console.dir(value);
});

prints out to the console:

{ [ValidationError: child "fromDate" fails because ["fromDate" references "toDate" which is not a date]]
  name: 'ValidationError',
  details:
   [ { message: '"fromDate" references "toDate" which is not a date',
       path: 'fromDate',
       type: 'date.ref',
       context: [Object] } ],
  _object:
   { fromDate: Sat Oct 24 2015 20:31:20 GMT-0400 (Eastern Daylight Time),
     toDate: '' },
  annotate: [Function] }

Metadata

Metadata

Assignees

Labels

supportQuestions, discussions, and general support

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions