joi
joi copied to clipboard
Nested objects validation not working as expected
Support plan
- *is this issue currently blocking your project?: yes
- *is this issue affecting a production system?: yes
Context
- node version: 14.17.0
- module version: 17.9.1
- environment : node
- *used with: express framework
- any other relevant information: Please test the below schema in https://joi.dev/tester/
How can we help?
I am expecting error in this case, but the validation is getting success.
Joi.object({
isTemplate: Joi.boolean().optional().valid(true, false),
fileId: Joi.string().optional(),
req:Joi.object({
template: Joi.alternatives().conditional('isTemplate', {
is: Joi.valid(true),
then: Joi.string().required(),
otherwise: Joi.string().optional(),
})
})
});
Sample data: {
isTemplate: true,
fileId: "435"
}
You need to add .required() to the req: Joi.object(/* ... */) part:
Joi.object({
isTemplate: Joi.boolean().optional().valid(true, false),
fileId: Joi.string().optional(),
req:Joi.object({
template: Joi.alternatives().conditional('isTemplate', {
is: Joi.valid(true),
then: Joi.string().required(),
otherwise: Joi.string().optional(),
})
}).required()
});
@MallRoy change .alternatives().conditional to .when and viola