Jasper
Jasper

Reputation: 534

Validation on relation between parameters, with hapijs/joi

I am trying to validate optional parameters from a querystring that are depending on each other. In other words, when parameter A is added, parameter B is also needed. Both parameters are optional, so when neither of them are provided validation should also pass.

Should pass:

?a=1&b=2&c=0
?c=0

Should fail:

?a=1&c=0
?b=2&c=0

Can someone give me an example schema on how to solve this?

Upvotes: 3

Views: 1338

Answers (1)

ZeMoon
ZeMoon

Reputation: 20274

Have a look at Joi.object().and()

Joi.object().keys({
  a: Joi.number(),
  b: Joi.number(),
  c: Joi.number()
}).and('a', 'b');

Upvotes: 6

Related Questions