Skip to content

Commit 0f0d0db

Browse files
committed
Merge branch 'develop'
2 parents 3ba8604 + ca60602 commit 0f0d0db

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/Rules/Boolean.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use StellarWP\Validation\Contracts\ValidatesOnFrontEnd;
88
use StellarWP\Validation\Contracts\ValidationRule;
99

10+
use const FILTER_NULL_ON_FAILURE;
11+
1012
class Boolean implements ValidationRule, ValidatesOnFrontEnd, Sanitizer
1113
{
1214
/**
@@ -32,12 +34,13 @@ public static function fromString(string $options = null): ValidationRule
3234
/**
3335
* {@inheritDoc}
3436
*
37+
* @since 1.4.1 add is_bool check and FILTER_NULL_ON_FAILURE flag to prevent false positives
3538
* @since 1.4.0
3639
*/
3740
public function __invoke($value, Closure $fail, string $key, array $values)
3841
{
39-
if (!filter_var($value, FILTER_VALIDATE_BOOLEAN)) {
40-
$fail(sprintf(__('%s must be an boolean', '%TEXTDOMAIN%'), '{field}'));
42+
if (!is_bool(filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE))) {
43+
$fail(sprintf(__('%s must be a boolean', '%TEXTDOMAIN%'), '{field}'));
4144
}
4245
}
4346

tests/unit/Rules/BooleanTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function testRuleValidatesBooleans($value, $pass)
2424
}
2525

2626
/**
27+
* @since 1.4.1 updates tests to pass false-y values
2728
* @since 1.4.0
2829
*/
2930
public function booleansProvider(): array
@@ -36,15 +37,16 @@ public function booleansProvider(): array
3637
['true', true],
3738
['yes', true],
3839
['on', true],
40+
[false, true],
41+
[0, true],
42+
['0', true],
43+
['false', true],
44+
['no', true],
45+
['off', true],
3946

4047
// values that fail
41-
[false, false],
42-
[0, false],
43-
['0', false],
44-
['false', false],
45-
['no', false],
46-
['off', false],
4748
['abc', false],
49+
['123', false],
4850
];
4951
}
5052

0 commit comments

Comments
 (0)