Search code examples
typescriptjoi

Typescript Joi date validation


Is there any way to make Joi.date only accept YYYY-MM-DD format? I tried to use:

Joi.date().format('YYYY-MM-DD')

but I get an error

Property 'format' does not exist on type 'DateSchema'.ts(2339)

Solution

  • Please make sure you install both joi and @joi/date and extend Joi as shown below.

    const Joi = require('joi')
    .extend(require('@joi/date'));
    

    Then you should be able to use Joi.date().format('YYYY-MM-DD') without any error.