Joi: required oneOf field to be true

Viewed 316

I am using Joi for my validation schema.

I would like that at least one of theses values should be true.

a=true b=true //valid
a=true b=true //valid
a=false b=true //valid
a=false b=false //invalid

I tried this: but i get a dependency error..

Joi.object({
  a: Joi.boolean().when('b', {
    is: true,
    then: Joi.optional(),
    otherwise: Joi.required(),
  },
  b: Joi.boolean().when('a', {
    is: true,
    then: Joi.optional(),
    otherwise: Joi.required(),
  },
});

Thanks!

1 Answers

Recently, the following code satisfied my problem in a similar situation:

Joi.object({
  a: Joi.bool().required(),
  b: Joi.bool().when('a', {
    is: false,
    then: Joi.invalid(false),
  }).required(),
});

You might want to play around with required() option needed in a different manner.

Related