The validation for boolean type is returning a false positive when validating this strings: 'true' and 'false'.
const string = 'true'
const schema = Joi.boolean()
const validation = Joi.validate(string, schema)
validation.error === null
validation.error !== null
This is the expected behavior according to the API documentation-
If the validation convert option is on (enabled by default), a string (either "true" or "false") will be converted to a boolean if specified.
If you want "true" and "false" to error you can disable type conversion with .strict() or options:
Joi.boolean().strict().validate('true'); // ValidationError: "value" must be a boolean
Joi.boolean().validate('true', { convert: false }); // ValidationError: "value" must be a boolean
Thank you @WesTyler.
It's confusing to validate a string as a boolean when expecting a type validation by default, but it's true that this behavior is reported in the documentation.
Thank you for your time.
Joi is originally made to be used on web scenarios, and query strings come as, well, strings... so it's the default behavior 馃檪 It's no less surprising but at least now you know why 馃槈
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.
Most helpful comment
Thank you @WesTyler.
It's confusing to validate a string as a boolean when expecting a type validation by default, but it's true that this behavior is reported in the documentation.
Thank you for your time.