node.js - 如何使用 express-validator 检查是否存在至少一个参数

标签 node.js validation express

我在我的 Nodejs 应用中使用 expressexpress-validator。我想检查是否存在至少一个传入参数。它是一种或组合。

假设我的服务接受 2 个参数。我想确定至少其中一个是由客户提供的。

下面的代码只适用于一个。但是我也不知道怎么做。

req.checkBody('param1', 'Mandatory field param1 not populated').notEmpty();

最佳答案

假设您要更新具有 idstatuscontent 的模型...例如社交媒体帖子.您的 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/

相关文章:

node.js - "section"在 github : what does it mean?

javascript - 如何在 Discord.js 中创建 "+mention"命令

javascript - Jquery 自定义验证无法正常工作

Node.js、Expressjs、Mongo、Jade : `new` action passing initialized mongo instance resulting in 'ReferenceError: jade is not defined'

android - 使用 Node.JS 服务器验证 Android 应用程序检索到的 Google 访问 token

javascript - 修改feathers hook.result中的响应状态码

Node.js - 在 Express 中从 Swig 迁移到 Nunjucks

c# - 从 C# 程序连接基于 node.js 的 socket.io WebSocket 服务器

jquery - 根据黑名单数组验证输入值

ruby-on-rails - Michael Hartl Rails 教程 : assert_not does exact opposite of what it should and I don't understand why