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

203
Visualizações
No errors with express-validator isEmpty

I am using express-validator 5.2.0 in a expressjs app. I implemented the validaiton on a form data. But it does not capture the errors and gives empty errors object. When validation runs it shows the "no error" when I submit the empty form. It should follow the error path and should display the error.

var express = require('express');
var router = express.Router();
const { check, validationResult } = require('express-validator/check');

router.post('/register', [
  check('firstName', 'First Name is required').isEmpty(),
  check('lastName', 'Last Name is required').isEmpty(),
  check('username', 'User Name is required').isEmpty(),
  check('password', 'Password is required').isEmpty()
], (req, res, next)=>{
  let errors = validationResult(req);
  if (!errors.isEmpty()) {
    console.log(errors.mapped());
    console.log("errors")
    return res.render('auth/register', { errors: errors.mapped() })
  }else{
    console.log('no errors')
    return res.render('auth/login');
  }

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

check('firstName', 'First Name is required').isEmpty() is enforcing firstName to be empty.
You will want to change that last part to .not().isEmpty(), so that you invert the isEmpty validator.

Might also be of your interest: https://github.com/express-validator/express-validator/issues/476

over 4 years ago · Santiago Trujillo Relatório

0

In your snippet you have not added a validtor to check if a value is not empty. You could achieve this by doing:

check('firstName', 'First Name is required').notEmpty()

.notEmpty() adds a validator to check if a value is not empty; that is, a string with a length of 1 or bigger.

https://express-validator.github.io/docs/validation-chain-api.html#notempty

over 4 years ago · Santiago Trujillo 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