Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!