From 137d1527244f8c1be4114dfedb230e2a72ec99cd Mon Sep 17 00:00:00 2001 From: Jon Waldstein Date: Tue, 15 Aug 2023 11:19:43 -0400 Subject: [PATCH 1/2] Fix: boolean rule to check if value is a boolean (#17) Co-authored-by: Jon Waldstein --- src/Rules/Boolean.php | 7 +++++-- tests/unit/Rules/BooleanTest.php | 13 +++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Rules/Boolean.php b/src/Rules/Boolean.php index c6dcd0c..f390178 100644 --- a/src/Rules/Boolean.php +++ b/src/Rules/Boolean.php @@ -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 { /** @@ -32,12 +34,13 @@ public static function fromString(string $options = null): ValidationRule /** * {@inheritDoc} * + * @unreleased 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}')); } } diff --git a/tests/unit/Rules/BooleanTest.php b/tests/unit/Rules/BooleanTest.php index 9ede234..84db040 100644 --- a/tests/unit/Rules/BooleanTest.php +++ b/tests/unit/Rules/BooleanTest.php @@ -36,15 +36,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], ]; } From ca60602b738ea4b1ca146a9b526c3f0337360222 Mon Sep 17 00:00:00 2001 From: Jason Adams Date: Tue, 15 Aug 2023 08:24:44 -0700 Subject: [PATCH 2/2] chore: prepeare for 1.4.1 release --- src/Rules/Boolean.php | 2 +- tests/unit/Rules/BooleanTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Rules/Boolean.php b/src/Rules/Boolean.php index f390178..b4dba06 100644 --- a/src/Rules/Boolean.php +++ b/src/Rules/Boolean.php @@ -34,7 +34,7 @@ public static function fromString(string $options = null): ValidationRule /** * {@inheritDoc} * - * @unreleased add is_bool check and FILTER_NULL_ON_FAILURE flag to prevent false positives + * @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) diff --git a/tests/unit/Rules/BooleanTest.php b/tests/unit/Rules/BooleanTest.php index 84db040..89dd4b4 100644 --- a/tests/unit/Rules/BooleanTest.php +++ b/tests/unit/Rules/BooleanTest.php @@ -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