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

215
Visualizações
Joi async custom validation

I need to validate this object:

{a: 'valueA', b: 'valueB'}

To validate 'a' i need to check if his value (valueA) is present in my db. If the value is not present then the object is not valid, otherwhise is valid. How can I create a custom async/await with Joi validator to achieve this?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can add external validation using the any.external() method, it receives a function as a parameter, so can be used to check the database for values.

The received functions' first parameter is the clone of the object you set to be validated. It runs after all other validations. If schema validation failed, no external validation rules are called.

const checkData = async (data) => {
  const res = await db.find({ name: data.a }) // or any method you use
  if (!res) {
    throw new Error('Data not found in db!')
  }
}

const schema = Joi.string().external(checkData)
await schema.validateAsync({ a: 'valueA', b: 'valueB' })

More information

about 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