【问题标题】:Send array from postman and validate with express validator in Node JS从邮递员发送数组并在 Node JS 中使用 express 验证器进行验证
【发布时间】:2021-01-18 19:30:43
【问题描述】:

在 Node js 中,我使用 express 验证器来验证 API 的请求正文。 我有一个文件validators.js,我在其中为 API 请求定义验证器。 我想验证inviteesArray,它需要采用以下格式:

//expected
[
  {"userId" :"07aeb7ff-bf11-4a64-b842-a6fe1aa0a7f6"},
  {"userId" :"7b89059a-3915-4d1d-b55c-077260f07021"}
];

我的验证者:

body('inviteesArray','inviteesArray is required!').if((value, { req }) => req.body.isInvited !== "false").isArray().notEmpty(),

邮递员中的请求,这给出了验证错误,我做错了什么?

**UPDATE**
when i change my validator with removing the if condition it works.
    body('inviteesArray','inviteesArray is required!').if((value, { req }) => console.log(value)).isArray().notEmpty(),

【问题讨论】:

  • 你可以在raw标签中使用Application/JSON
  • @aRvi 我本可以使用它,但我也有图像和视频上传请求,而 application/json 无法做到这一点。

标签: node.js arrays postman express-validator


【解决方案1】:

您必须在大括号内添加对象键以作为对象数组传递

KEY                       VALUE
invitesArray[0][userId]   first value
invitesArray[1][userId]   second value

【讨论】:

    猜你喜欢
    • 2020-07-30
    • 2021-09-22
    • 2014-12-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    相关资源
    最近更新 更多