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

303
Views
Pass two values ​to custom validation with express-validation

I need to receive two values ​​to search for a subdocument using the main ID of the document and then another ID of the subdocument.

I receive these ID's by the parameters and using express-validator I am using a custom function to verify that they exist in this way:

router.put('/:id/item/:idItem',   
    [        
        check('id','idItem').custom( idItemExists )
    ],
    validation,
    updateItem 
);

And in my function called idItemExists() I try to receive both values ​​but the second value, when passed to the console, returns everything that comes from the request:

let idItemExists = async ( id, idItem ) => { 
    console.log(idItem);
}

Response Capture

In this case I need to be able to receive both values ​​correctly in order to validate if both IDs are correct.

Thanks a lot.

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

0

this will not work since .custom takes a function that receives the value of the field being validated, as well as the express request, the location and the field path as mentioned in their docs.

validator(value, { req, location, path })

in your case you're trying to log the object that contains the req, location and the path.

in order to get both values inside a custom validator you could do the following:

 param() // when we omit fields, then we're getting all fields inside req.params
    .custom(params => { // here we ignore the second parameter { req, location, path }
        console.log(params) // { id: '10', idItem: '20' }
    })
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!