从邮递员发送数组并使用Node JS中的快速验证器进行验证

问题描述

在Node js中,我正在使用快速验证器来验证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(),

邮递员的请求,这会导致验证错误,我在做什么错了?

enter image description here

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

解决方法

您喜欢在括号内添加对象键以作为对象数组传递

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...