Send array from postman and validate with express validator in Node JS

754 views Asked by At

In Node js, i am using express validator to validate the request body of an API. I have one file validators.js where I define validators for an API request. I want to validate inviteesArray which needs to be in below format:

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

my validator:

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

request in postman, which gives validation error, what am I doing wrong?

enter image description here

**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(),
1

There are 1 answers

0
aRvi On

You hove to add object keys inside braces to pass as an array of object

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