Sunday, 10 November 2019

Joi validator for one of the keys

I can provide several params including age, day, month, year, gender when requesting for user's information. I want to only provide either day or month in the joi validation and how to approach this? I did some research and found that you can use object.oxor to define an exclusive relationship between a set of keys where only one is allowed but none are required. However, if my schema looks something like this:const schema = Joi.object({age: Joi.any(),day: Joi.any(),month: Joi.any(),year: Joi.any(),gender: Joi.any()})​My thinking is that I can do something like this:const schema = Joi.object({age: Joi.any(),day: Joi.any(),month: Joi.any(),year: Joi.any(),gender: Joi.any()}).oxor('day', 'month')​How can I ensure either day or month is allowed, but not both? Thank you!

Submitted November 11, 2019 at 06:07AM by throwawayLoser112

No comments:

Post a Comment