Joi: Joi.boolean() false positive to strings 'true' ans 'false'

Created on 26 Feb 2019  路  4Comments  路  Source: sideway/joi

Context

  • node version: v8.15.0
  • joi version: 14.3.1
  • environment (node, browser): node
  • used with (hapi, standalone, ...): express
  • any other relevant information: no

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

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)

Which result you had ?

validation.error === null

What did you expect ?

validation.error !== null

non issue

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.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

a-c-m picture a-c-m  路  3Comments

jamesdixon picture jamesdixon  路  4Comments

sergibondarenko picture sergibondarenko  路  3Comments

n-sviridenko picture n-sviridenko  路  3Comments

farwayer picture farwayer  路  3Comments