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

301
Visualizações
Express Validator Schema add sequence custom validator

Is there anyway to add sequence check for userName

here is my schema validator:

const validatorSchema = {
 userName: {
    custom: {
      options: (value) => {
        if (dataUtils.isEmpty(value) || value === null) {
          return false;
        }
      },
      errorMessage: "userName field is empty",
    },
    custom: {
      options: (value) => {
        return User.findOne({
          where: {
            userName: value,
          },
        }).then((user) => {
          if (user == null) {
            return true;
          } else {
            return Promise.reject(user);
          }
        });
      },
      errorMessage: "User already existed",
    },
    custom: {
      options: (value) => {
        if (dataUtils.whiteSpaceChecker(value) && value != null) {//not allow whiteSpace between
          return false;
        }
      },
      errorMessage: "Wrong format userName",
    },
   
  },
}

My request body:

{
 "userName": "notExistedUser",
 "password": "samplePassword"
}

I do it by following this tutorial
But I want do more than check Existed User only and it didn't work as expected
Response always send

"errors": [
                {
                    "value": "notExistedUser",
                    "msg": "Wrong format userName",
                    "param": "userName",
                    "location": "body"
                }
          ]

Note: I use this schema in middleware

const { body, checkSchema, validationResult } = require("express-validator");

app.post('/signup', checkSchema(validatorSchema), (req, res) => {
    // Validate incoming input
    const errors = validationResult(req);

    if (!errors.isEmpty()) {
        return res.status(400).json({
            errors: errors
        });
    }

    next();
})

Thanks for reading this article,I hope can reach the solution for this !

about 4 years ago · Juan Pablo Isaza
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