Request Body is like
{
"name":"nazmul Haque",
"age" : "26",
"favoriteBooks" :
[
{
"name": "A Murder is announced",
"writer":"Agatha Christie",
"price" :700
},
{
"name": "Srikanto",
"writer":"Sarat Chandra Chattopadhyay",
"price" :500
}
]
}
Now I want to validate name key exists or not and the length must be in 256 and price can not be empty of every object of the favorite books. and how I do that with express validator without custom method.
like if our req body will look like in below
[
{
"name": "A Murder is announced",
"writer":"Agatha Christie",
"price" :700
},
{
"name": "Srikanto",
"writer":"Sarat Chandra Chattopadhyay",
"price" :500
}
]
than we can validate it like
body().isArray().withMessage("body is not an array"),
body("*.name", "name can not be null").exists().notEmpty().isLength({max:256}),
body("*.price")
.exists()
.notEmpty()
.withMessage("price can not be null"),
I can not do it with the process in above For 1st Req Body because there array of Object inside of a object
export const complexValidation = [
body('name', "Name must be a string")
.isLength({ min: 1 }),
body('age', "age must be a integer")
.isInt(),
body('favoriteBooks', "favoriteBooks must be an array with min single value")
.isArray({ min: 1 }),
body('favoriteBooks.*.name', "Name must be a string")
.isLength({ min: 1 }),
body('favoriteBooks.*.writer', "Name must be a string")
.isLength({ min: 1 }),
body('favoriteBooks.*.price', "Name must be a string")
.isNumeric()
]