Joi: Joi doesn't validate `allow()`

Created on 23 May 2017  路  3Comments  路  Source: sideway/joi

Context

  • node version: 7.10.0
  • joi version: 10.5.0
  • environment (node, browser): browser
  • used with (hapi, standalone, ...): -
  • any other relevant information: -

What are you trying to achieve or the steps to reproduce ?

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);

Which result you had ?

{ error: null, .. }

What did you expect ?

{ error: <error>, .. }
support

Most helpful comment

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'])

All 3 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PaunPrashant picture PaunPrashant  路  3Comments

jamesdixon picture jamesdixon  路  4Comments

Dreamystify picture Dreamystify  路  4Comments

JbIPS picture JbIPS  路  4Comments

ashrafkm picture ashrafkm  路  3Comments