Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
How can I validate that req.body has a valid date string using Joi in node JS

I have made a validation middleware whose code is given below like this.

export const validateMiddleware = async (req, res, next) => {

    const validateExpression = Joi.object()
        .keys({
            'startDate': Joi.string()
                .optional()
                .allow(''),
             'endDate': Joi.string()
                 .optional()
                 .allow('')
        });

    const {
        error
    } = validateExpression.validate(req.query, {
        'convert': false,
        'abortEarly': false
    });

    if (error) {
       // Return response regarding that error.
    } else {
        next();
    }
};

I need to validate that startDate and endDate fields should be string of format YYYY-MM-DD also need to validate that startDate < endDate date value. How can I achieve this using Joi in Node JS ?.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think this code will solve your problem:

const validateExpression = Joi.object()
  .keys({
    'startDate': Joi.date()
      .format("YYYY-MM-DD")
      .optional()
      .allow(''),
    'endDate': Joi.date()
      .format("YYYY-MM-DD")
      .optional()
      .allow('')
      .min(Joi.ref('startDate'))
  });
about 4 years ago · Juan Pablo Isaza Report

0

Try this:


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


export const validateMiddleware = async (req, res, next) => {

    const validateExpression = Joi.object()
        .keys({
            'startDate': Joi.date()
                .format("YYYY-MM-DD")
                .strict(false) // required if 'convert' option is set to "false"
                .optional()
                .allow(''),
             'endDate': Joi.date()
                .format("YYYY-MM-DD")
                .greater(Joi.ref("startDate")) // checks if it is greater than startDate, throws error if now.
                .strict(false) // can be omitted if 'convert' option is set to "true"
                .optional()
                .allow('')
        });

    const {
        error
    } = validateExpression.validate(req.query, {
        'convert': false,
        'abortEarly': false
    });

    if (error) {
       // Return response regarding that error.
    } else {
        next();
    }
};

DO NOT forget to call strict(false). Otherwise passing date as string won't work, since you have 'convert': false. Or alternatively, you can set 'convert': true (which is the default value anyway) and you won't need to call string(false) on startDate and endDate.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!