In my project, I want to validate the missing fields during post method. Fields in my table look like:
{
name: string, required: true
products: [
{
name: string, required: true
quantity: number, required: true
rate: number
}
]
Now I declare validation for the name field as follows:
req.checkBody('name','Missing params').notEmpty();
It works properly, but the array is not working and the code is:
req.checkBody(products: 'name','Missing params').notEmpty();
req.checkBody(products: 'quantity','Missing params').notEmpty();
When you want to validate an array in a nested object, you have to pass an array. Instead of doing this:
req.checkBody(products: 'name','Missing params').notEmpty();
req.checkBody(products: 'name','Missing params').notEmpty();
, you should do this:
req.checkBody(['products', 0, 'name'],'Missing params').notEmpty();
req.checkBody(['products', 0, 'quantity'],'Must be an integer').isInt();
, and so on.