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

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

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 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