Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda