Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Joi, validating that a boolean is true

Is it possible to validate that a boolean is true using Joi? I've tried using allow, valid and invalid without any luck.

like image 736
anthonator Avatar asked Oct 08 '15 15:10

anthonator


People also ask

Should I use Joi or express validator?

Joi can be used for creating schemas (just like we use mongoose for creating NoSQL schemas) and you can use it with plain Javascript objects. It's like a plug n play library and is easy to use. On the other hand, express-validator uses validator. js to validate expressjs routes, and it's mainly built for express.

What is Joi validation?

Hapi Joi is an object schema description language and validator for JavaScript objects. With Hapi Joi, we create blueprints or schemas for JavaScript objects (an object that stores information) to ensure validation of key information.


1 Answers

Have you tried something like this:

var schema = Joi.boolean().invalid(false);

Using that schema, the following all populate the error property:

Joi.validate(false, schema);
Joi.validate('false', schema);
Joi.validate('no', schema);
Joi.validate('off', schema);
Joi.validate(0, schema);
like image 93
jrmce Avatar answered Oct 03 '22 08:10

jrmce