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

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

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