I am writing an update endpoint in nodeJs. I want to make sure that additional fields wont be allowed, if certain flags are checked.
all_restaurant: Joi.boolean().required(),
merchant_id: Joi.when('all_restaurant',{'is':false,
then: Joi.string().required(), otherwise:Joi.disallow()})
If all_restaurant is true, I need to throw an error when the user tries to include any merchant_id, but it still allowing me to update. I tried using strict() after the field and in the top level but nothing is working. is there any way to achieve that?
You can use Joi.forbidden instead of Joi.disallow:
const schema = Joi.object({
all_restaurant: Joi.boolean().required(),
merchant_id: Joi.string().when('all_restaurant', {
is: false,
then: Joi.required(),
otherwise: Joi.forbidden()
})
})
As the name suggests, forbidden marks a key as forbidden.