Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda