I am trying to implement validation on my express Router, The problem is When i pass {title:""} the express-validator didn't throw error , but when i pass {title:" "} it works .
exports.postValidatorCheck = [
check("title", "The title must not we empty").isEmpty(),
check("title", "The Length of the Title should be in greater then 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.
Validation should use the negative:
check("title", "The title must not we empty").not().isEmpty()
This will make sure the title
is not empty, which is what I think you intended.