Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonTheAdams committed Aug 15, 2023
2 parents 3ba8604 + ca60602 commit 0f0d0db
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/Rules/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use StellarWP\Validation\Contracts\ValidatesOnFrontEnd;
use StellarWP\Validation\Contracts\ValidationRule;

use const FILTER_NULL_ON_FAILURE;

class Boolean implements ValidationRule, ValidatesOnFrontEnd, Sanitizer
{
/**
Expand All @@ -32,12 +34,13 @@ public static function fromString(string $options = null): ValidationRule
/**
* {@inheritDoc}
*
* @since 1.4.1 add is_bool check and FILTER_NULL_ON_FAILURE flag to prevent false positives
* @since 1.4.0
*/
public function __invoke($value, Closure $fail, string $key, array $values)
{
if (!filter_var($value, FILTER_VALIDATE_BOOLEAN)) {
$fail(sprintf(__('%s must be an boolean', '%TEXTDOMAIN%'), '{field}'));
if (!is_bool(filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE))) {
$fail(sprintf(__('%s must be a boolean', '%TEXTDOMAIN%'), '{field}'));
}
}

Expand Down
14 changes: 8 additions & 6 deletions tests/unit/Rules/BooleanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function testRuleValidatesBooleans($value, $pass)
}

/**
* @since 1.4.1 updates tests to pass false-y values
* @since 1.4.0
*/
public function booleansProvider(): array
Expand All @@ -36,15 +37,16 @@ public function booleansProvider(): array
['true', true],
['yes', true],
['on', true],
[false, true],
[0, true],
['0', true],
['false', true],
['no', true],
['off', true],

// values that fail
[false, false],
[0, false],
['0', false],
['false', false],
['no', false],
['off', false],
['abc', false],
['123', false],
];
}

Expand Down

0 comments on commit 0f0d0db

Please sign in to comment.