TypeError: req.checkBody(…).optional(…).isDate is not a function

I am facing issues using the express validator specifically it is the isDate function. I have taken steps to use expressvalidator, the bodyparse, validator module etc. All routes are after this only.. Environment is Node + Express.

The issue is on the usage of

"req.checkBody('date_of_birth', 'Invalid date').optional({ checkFalsy: true }).isDate();" 

and I keep getting the following error.

TypeError: req.checkBody(...).optional(...).isDate is not a function
    at exports.author_create_post (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/controllers/authorController.js:47:81)
    at Layer.handle [as handle_request] (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
    at /Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:174:3)
    at router (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:317:13)
    at /Users/svitaworld/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:275:10)
    at /Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:635:15

app.js

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: false
}));
app.use(expressValidator()); // Add this after the bodyParser middlewares!
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);
app.use('/catalog', catalog); // Add catalog routes to middleware chain.

in one of my controllers, I am using the isDate() method to do some validation on date that I have separately defined int he AuthorSchema.

var AuthorSchema = Schema(   {
  first_name: {type: String, required: true, max: 100},
  family_name: {type: String, required: true, max: 100},
  date_of_birth: {type: Date},
  date_of_death: {type: Date},   
} );

Now to handle the post requests I have this code in the controller:

authorController.js -- line from line 41-48

// Handle Author create on POST
exports.author_create_post = function(req, res, next) {
  console.log("DEBUG: starting in exports.author_create_post");
  req.checkBody('first_name', 'First name must be specified.').notEmpty(); 
  req.checkBody('family_name', 'Family name must be specified.').notEmpty();
  req.checkBody('family_name', 'Family name must be alphanumeric text.').isAlpha();
  req.checkBody('date_of_birth', 'Invalid date').optional({ checkFalsy: true }).isDate(); // Error is on this usage of isDate()
  req.checkBody('date_of_death', 'Invalid date').optional({ checkFalsy: true }).isDate();

Answers:

Answer

isDate() has been removed from validator.js. You can see this commit on GitHub for more information. express-validator uses validator.js for validation.

You can make a custom validator to check for valid dates. For the new API:

check('date').custom(isValidDate).withMessage('the date must be valid');

For the legacy API:

app.use(expressValidator({
  customValidators: {
    isValidDate: isValidDate
  }
}));

when you apply the middleware (in app.js or something similar) and for checking:

req.checkBody('date', 'the date must be valid').isValidDate();

isValidDate() has to be written by yourself. Here is an example:

function isValidDate(value) {
  if (!value.match(/^\d{4}-\d{2}-\d{2}$/)) return false;

  const date = new Date(value);
  if (!date.getTime()) return false;
  return date.toISOString().slice(0, 10) === value;
}

This checks for dates with the yyyy-mm-dd format. It was taken from this answer. There are also plenty of other answers for different formats here on Stack Overflow:

Or use moment's isValid().

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.