Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
How I validate a array of object inside a Object in request body with express validator

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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()
]

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!