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

187
Views
Express validator custom function

I'm trying to write a custom validator for an express API. I can't figure out how I can reject incorrect values from the custom validator. Here's what I have:


function validateInput(value) {
    if (!(
        (check(value).isIn(utils.getNames()) ||
        check(value).isIn(utils.getIds()) ||
        check(value).isIn(utils.getHexIds()))
    )) {
        return Promise.reject("Invalid input");
    }
}

router.get('/endpoint',
    query("input").custom(async (value) => {
        await validateInput(value).catch()
    }),
    async (req, res) => {
        try {
            validationResult(req).throw();
        } catch (err) {
            res.status(400).json({"error": "A valid value for input must be passed"});
            return;
        }
   }

I have a feeling I"m not correctly handling the promise correctly, but I can't figure out the right solution either. Here are the docs for the custom validator: https://express-validator.github.io/docs/custom-validators-sanitizers.html

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

0

Looks like you are over-complicating the validator.

First, .catch() should take an argument as a general rule.

Here, you do want the promise to reject in case of invalidation, so you don't need to catch.

How about:

query("input").custom(validateInput)
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!