npm install joi-relative-date-validator
- First import @hapi/Joi
import Joi from @hapi/Joi
Note: This module does not install Joi
automatically. You need to add it to your dependency.
- Import module
import relativeDateValidator from 'joi-relative-date-validator'
- Extend Joi
const custom = Joi.extend(relativeDateValidator());
const schema: Schema = custom.relativeDate().within(2, 'd');
- Generate Schema
const schema: Schema = custom.relativeDate().within(2, 'd');
- And finally validate
schema.validate('2018-10-31');
schema.validate(new Date(2018-10-31T00:00:00));
schema.validate(moment());
More examples in src/index.test.ts file.
- Joi.Date is base type of this validator.
- Internally it uses Moment.js, so you can pass any moment parsable date to validator function