From 500472808c1434c1ca06bc1f8b607932bf953a78 Mon Sep 17 00:00:00 2001 From: Willem Wollebrants Date: Mon, 5 Jul 2021 22:14:24 +0200 Subject: [PATCH 1/3] detect numeric array when keys are out of order (#332) --- src/Valitron/Validator.php | 13 ++++++++----- tests/Valitron/ValidateTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index ed04d77..0c0a844 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -420,9 +420,8 @@ class Validator */ protected function validateIn($field, $value, $params) { - $isAssoc = array_values($params[0]) !== $params[0]; - if ($isAssoc) { - $params[0] = array_keys($params[0]); + if ($this->isAssociativeArray($params[0])) { + $params[0] = array_keys($params[0]); } $strict = false; @@ -443,8 +442,7 @@ class Validator */ protected function validateListContains($field, $value, $params) { - $isAssoc = array_values($value) !== $value; - if ($isAssoc) { + if ($this->isAssociativeArray($value)) { $value = array_keys($value); } @@ -1566,4 +1564,9 @@ class Validator $me->mapFieldRules($field, $rules[$field]); }, array_keys($rules)); } + + private function isAssociativeArray($input){ + //array contains at least one key that's not an can not be cast to an integer + return count(array_filter(array_keys($input), 'is_string')) > 0; + } } diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index d086ea8..58b34da 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -4,6 +4,7 @@ use Valitron\Validator; class ValidateTest extends BaseTestCase { + public function testValidWithNoRules() { $v = new Validator(array('name' => 'Chester Tester')); @@ -2920,6 +2921,35 @@ class ValidateTest extends BaseTestCase $v->rule('optional', 'address'); $this->assertTrue($v->validate()); } + + /** + * + * @see https://github.com/vlucas/valitron/issues/332 + */ + public function testInRuleSearchesValuesForNumericArray(){ + + $v = new Valitron\Validator(array('color' => 'purple')); + + $v->rules(array( + 'in' => array( + array('color', array(3=>'green', 2=>'purple')) + ) + )); + + $this->assertTrue($v->validate()); + } + + public function testInRuleSearchesKeysForAssociativeArray(){ + $v = new Valitron\Validator(array('color' => 'purple')); + + $v->rules(array( + 'in' => array( + array('color', array('c-3'=>'green', 'c-2'=>'purple')) + ) + )); + + $this->assertFalse($v->validate()); + } } function sampleFunctionCallback($field, $value, array $params) From be8cb9e074ce2dbf1a47d8a61a450f8fb2712420 Mon Sep 17 00:00:00 2001 From: Willem Wollebrants Date: Tue, 6 Jul 2021 10:58:22 +0200 Subject: [PATCH 2/3] add parameter to force in and listContains to look at array keys --- src/Valitron/Validator.php | 13 +++++++++++-- tests/Valitron/ValidateTest.php | 15 +++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 0c0a844..6555384 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -420,7 +420,11 @@ class Validator */ protected function validateIn($field, $value, $params) { - if ($this->isAssociativeArray($params[0])) { + $forceAsAssociative = false; + if (isset($params[2])) { + $forceAsAssociative = (bool) $params[2]; + } + if ($this->isAssociativeArray($params[0]) || $forceAsAssociative) { $params[0] = array_keys($params[0]); } @@ -442,7 +446,12 @@ class Validator */ protected function validateListContains($field, $value, $params) { - if ($this->isAssociativeArray($value)) { + $forceAsAssociative = false; + if (isset($params[2])) { + $forceAsAssociative = (bool) $params[2]; + } + + if ($this->isAssociativeArray($value) || $forceAsAssociative) { $value = array_keys($value); } diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 58b34da..de06b61 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -2940,7 +2940,7 @@ class ValidateTest extends BaseTestCase } public function testInRuleSearchesKeysForAssociativeArray(){ - $v = new Valitron\Validator(array('color' => 'purple')); + $v = new Valitron\Validator(array('color' => 'c-2')); $v->rules(array( 'in' => array( @@ -2948,7 +2948,18 @@ class ValidateTest extends BaseTestCase ) )); - $this->assertFalse($v->validate()); + $this->assertTrue($v->validate()); + } + + public function testInRuleSearchesKeysWhenForcedTo(){ + $v = new Valitron\Validator(array('color' => 2)); + + $v->rules(array( + 'in' => array( + array('color', array('3'=>'green', '2'=>'purple'), null, true) + ) + )); + $this->assertTrue($v->validate()); } } From d12a77deed0c92da2ea9a05e13de4a870dc346b4 Mon Sep 17 00:00:00 2001 From: Willem Wollebrants Date: Tue, 6 Jul 2021 14:16:59 +0200 Subject: [PATCH 3/3] check first (#332) --- src/Valitron/Validator.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 6555384..60b1603 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -424,7 +424,8 @@ class Validator if (isset($params[2])) { $forceAsAssociative = (bool) $params[2]; } - if ($this->isAssociativeArray($params[0]) || $forceAsAssociative) { + + if ($forceAsAssociative || $this->isAssociativeArray($params[0])) { $params[0] = array_keys($params[0]); } @@ -451,7 +452,7 @@ class Validator $forceAsAssociative = (bool) $params[2]; } - if ($this->isAssociativeArray($value) || $forceAsAssociative) { + if ($forceAsAssociative || $this->isAssociativeArray($value)) { $value = array_keys($value); }