I have been working on implementing validation using express Router. The issue I encountered is that when I pass {title:""}, the express-validator does not throw an error. However, when I pass {title:" "}, it works properly.
exports.postValidatorCheck = [
check("title", "The title must not be empty").isEmpty(),
check("title", "The length of the title should be greater than 10").isLength({
min: 10,
max: 1500
}),
function(req, res, next) {
let errors = validationResult(req);
console.log(errors);
if (!errors.isEmpty()) {
const firstError = errors.errors.map(err => err.msg)[0];
return res.status(400).json({ error: firstError });
}
next();
}
];
JSON file:
{
"title":"",
"body":"This is a new Post"
}
No error
JSON file
{
"title":" ",
"body":"This is new post"
}
Error as expected.