Reputation: 2396
While defining my route, I would like to be able to validate if an incoming parameter is an array and raise an error if it isn't. I have so far been using express-form
for all validations and can't seem to find a way to validate arrays in it. I know I can use express-validate
for a doing this but I would like to keep using what I've been using so far so that everything can look uniform.
app.post(
'/addUserInfo',
form(
field('userID').trim().required().is(idRegex)
// Need similar check for 'items' field here
),
offline.addUserInfo
);
Upvotes: 0
Views: 149
Reputation: 5704
It's in the docs!
You can use:
Update As mentioned in comment the value is string so needs to be parsed to array
field('items').custom(function(value){
// check if value is array, throw error if not
//if(value instanceof Array) return;
if(JSON.parse(value) instanceof Array) return;
throw new Error('The field "items" is not an array');
});
Upvotes: 1