express-validator to validate parameter which is an array
You may need to create your own custom validator;
expressValidator = require('express-validator');
validator = require('validator');
app.use(expressValidator({
customValidators: {
isArray: function(value) {
return Array.isArray(value);
},
notEmpty: function(array) {
return array.length > 0;
}
gte: function(param, num) {
return param >= num;
}
}
}));
req.checkBody('category', 'category cannot be empty').isArray().notEmpty();
Try this:
router.post('/your-url',
[
check('category').custom((options, { req, location, path }) => {
if (typeof category === 'object' && category && Array.isArray(category) && category.length) {
return true;
} else {
return false;
}
})
],
controller.fn);