Using Joi, validating that a boolean is true

anthonator picture anthonator · Oct 8, 2015 · Viewed 12.9k times · Source

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

Answer

jrmce picture jrmce · Oct 8, 2015

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