Javascript {key:";}和{key:";},json文件之间的区别?
我正在尝试在我的快速路由器上实现验证,问题是当我传递{title:}时,快速验证程序没有抛出错误,但当我传递{title:}时,它会工作Javascript {key:";}和{key:";},json文件之间的区别?,javascript,json,express,middleware,express-validator,Javascript,Json,Express,Middleware,Express Validator,我正在尝试在我的快速路由器上实现验证,问题是当我传递{title:}时,快速验证程序没有抛出错误,但当我传递{title:}时,它会工作 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
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文件:
{
"title":"",
"body":"This is a new Post"
}
无误
JSON文件
{
"title":" ",
"body":"This is new post"
}
错误如预期 首先,“
是一个空字符串<代码>“”不是;它包含一个空格字符。如果要将任何空格计算为空,应使用
至于您的实际问题,您测试的是
isEmpty()
,而您应该测试的是not()。isEmpty()
验证应使用负数:
check("title", "The title must not we empty").not().isEmpty()
这将确保标题
不是空的,这是我认为您想要的。因此,当我传递{title:}时,express validator应该抛出一个错误,但它没有,它验证标题并继续。