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

153
Views
Joi nested when
const schema = {
    a: Joi.any(),
    b: Joi
      .boolean()
      .default(false),
    c: Joi
      .boolean()
      .default(false)
}

How can I fix the abovementioned Joi schema to matches the following rules

  • One of b or c can be true at the same time
  • When b or c is true, a is forbidden otherwise a is required
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Joi.object( {
  a: Joi
      .boolean()
      .default(false),
  b: Joi
      .boolean()
      .default(false),
  c: Joi.string().when(
    'a', {
      is: false,
      then: Joi.when(
        'b', {
          is: false,
          then: Joi.string().required()
        }
      )
    }
  ),
})

I tried nested when before asking question, but it not working before because of the order of rules

Joi.object( {
  c: Joi.string().when(
    'a', {
      is: false,
      then: Joi.when(
        'b', {
          is: false,
          then: Joi.string().required()
        }
      )
    }
  ),
  a: Joi
      .boolean()
      .default(false),
  b: Joi
      .boolean()
      .default(false),
})

the default value in the second schema is not set when the when is checking, So If you validate an empty object {}, the first schema has Validation Error: "c" is required error while the second Passed with {a: false, b: false}

I opened an issue here https://github.com/sideway/joi/issues/2683

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