简体   繁体   English

Node.js表示验证器验证req.body不为空

[英]Node.js express validator validate req.body is not empty

Is there any elegant way to use express-validator to validate the req.body I got is not empty? 是否有任何优雅的方式使用express-validator来验证req.body我得到的不是空的?

I didn't see any appropriate validator in here: https://github.com/chriso/validator.js 我在这里没有看到任何合适的验证器: https//github.com/chriso/validator.js

And I prefer not to use (Object.keys(req.body).length == 0) 我不想使用(Object.keys(req.body)。length == 0)

Is there any custom validator I should build to solve this? 我应该建立任何自定义验证器来解决这个问题吗?

Thanks 谢谢

For latter question 2. How to create a conditional validation (at least one of 'a','b','c' params is required). 对于后一个问题2.如何创建条件验证(至少需要'a','b','c'参数之一。 Not tested but something like below should work: 没有测试,但下面的东西应该工作:

var hasRequiredField = Object.keys(req.body).some(function(key) {
return ['a', 'b', 'c'].indexOf(key) >= 0;
});

if(hasRequiredField) {
} else {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM