Skip to content

Commit 32bac2f

Browse files
authored
Merge pull request #3092 from AnslemHack/bug-coerceStringsWithEmptySpaces
fix: allow coercion of string booleans with trailing whitespace
2 parents 1b923c1 + aa1f2b0 commit 32bac2f

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/types/boolean.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ module.exports = Any.extend({
4545
}
4646

4747
if (typeof value === 'string') {
48-
const normalized = schema._flags.sensitive ? value : value.toLowerCase();
48+
const trimmedValue = value.trim();
49+
const normalized = schema._flags.sensitive ? trimmedValue : trimmedValue.toLowerCase();
4950
value = normalized === 'true' ? true : (normalized === 'false' ? false : value);
5051
}
5152

test/types/boolean.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ describe('boolean', () => {
3131
]);
3232
});
3333

34+
it('converts boolean string with trailing spaces to a boolean', () => {
35+
36+
Helper.validate(Joi.boolean(), [
37+
['true ', true, true],
38+
['false ', true, false]
39+
]);
40+
});
41+
3442
it('does not convert boolean string to a boolean in strict mode', () => {
3543

3644
Helper.validate(Joi.boolean().strict(), [

0 commit comments

Comments
 (0)