Joi doesn't validate the following object correctly:
const Joi = require('joi');
const activities = ['active', 'inactive'];
const routeSchema = {
query: Joi.object({
activity: Joi.any().allow(activities),
}),
};
const query = { activity: '2' };
const result = Joi.validate({ query }, routeSchema);
// it should be invalid
console.log(result);
{ error: null, .. }
{ error: <error>, .. }
What Joi.any().allow(activities) means is that activity can be of any type and also allow 'active' and 'inactive'.
If you want to say only allow 'active' and 'inactive' try Joi.string().only(['active', 'inactive'])
@AdriVanHoudt you're right. thank you for the fast answering!
np glad I could help!
Most helpful comment
What
Joi.any().allow(activities)means is thatactivitycan be of any type and also allow'active'and'inactive'.If you want to say only allow
'active'and'inactive'tryJoi.string().only(['active', 'inactive'])