Skip to content

Conversation

@AnslemHack
Copy link

@AnslemHack AnslemHack commented Oct 28, 2025

Description:

This PR addresses issue #3091 by improving the coercion behavior of Joi.boolean() for string inputs with trailing whitespace.

Problem
Currently, Joi.boolean().validate('true ') fails with a validation error, even though Joi.number().validate('123 ') successfully coerces the string to a number. This inconsistency creates friction when validating user input from forms, query params, or loosely formatted sources where trailing whitespace is common.

Solution
This update trims string inputs before coercion in the boolean type, aligning its behavior with number. It ensures that:

js
Joi.boolean().validate('true ') // → { value: true }
Joi.boolean().validate('false ') // → { value: false }
Tests
Unit tests have been added to cover:

"true " → true

"false " → false

" true " → true

"false" → false (unchanged)

"yes" → still invalid

Documentation
No breaking changes. Behavior is now more permissive and consistent with other coercive types.

@AnslemHack
Copy link
Author

@Marsup here's a small PR to fix boolean validation around trailing input trims values and enforces 'true'/'false' only. Mind taking a look?

@Marsup Marsup added this to the 18.0.2 milestone Nov 19, 2025
@Marsup Marsup added the bug Bug or defect label Nov 19, 2025
@Marsup Marsup self-assigned this Nov 19, 2025
@Marsup Marsup merged commit 32bac2f into hapijs:master Nov 19, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Bug or defect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants