我在我的 Nodejs 应用中使用 express 和 express-validator。我想检查是否存在至少一个传入参数。它是一种或组合。
假设我的服务接受 2 个参数。我想确定至少其中一个是由客户提供的。
下面的代码只适用于一个。但是我也不知道怎么做。
req.checkBody('param1', 'Mandatory field param1 not populated').notEmpty();
最佳答案
假设您要更新具有 id
、status
和 content
的模型...例如社交媒体帖子.您的 Controller 可能支持更新模型的状态
或其内容
。因此,您可以执行以下操作:
export const updateModelValidation = [
param('id').exists().isNumeric(), // <-- required model identifier
oneOf( // <-- one of the following must exist
[
body('status').exists().isString(),
body('content').exists().isString(),
],
),
];
关于node.js - 如何使用 express-validator 检查是否存在至少一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39756415/