是否有类似的验证:
req.checkBody('property', 'should be present').isPresent();
值本身并不重要。
现在我使用这个解决方案:
.isLength()
更新
- .isLength()
是一个糟糕的解决方案,因为它测试该值是否是字符串。
- 并且您无法从浏览器发送“未定义”的 POST 值,因为 JSON.stringify 方法会忽略此类属性。
因此针对 !== undefined
的验证应该足够了。
最佳答案
您使用express validator吗?模块?
如果是,这对您有用吗?
req.checkBody('postparam', 'Invalid postparam').notEmpty()
更新
根据文档,不存在您想要的验证器。创建您的自定义解决方案可能是一种解决方案:
expressValidator({
customValidators: {
isPresent: function(value) {
return (typeof value !== 'undefined');
}
}
})
关于node.js - express-validator/node-validator : how to validate that property exists, 并且应该存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40136139/