From ab82f418a7ec3ae766cbe28eb10b5c30bf390420 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 3 Aug 2024 10:04:26 +0900 Subject: [PATCH 1/2] test: add tests for differs and matches --- tests/system/Validation/RulesTest.php | 88 +++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/tests/system/Validation/RulesTest.php b/tests/system/Validation/RulesTest.php index 7c69fa24838e..c39304461d61 100644 --- a/tests/system/Validation/RulesTest.php +++ b/tests/system/Validation/RulesTest.php @@ -336,6 +336,50 @@ public function testMatchesNested(array $data, bool $expected): void $this->assertSame($expected, $this->validation->run($data)); } + public function testMatchesWithDotArrayPass(): void + { + $rules = [ + 'name' => 'permit_empty', + 'emailAddress' => 'permit_empty|valid_email', + 'alias.*' => 'permit_empty|matches[name]', + ]; + $this->validation->setRules($rules); + + $data = [ + 'name' => 'Princess Peach', + 'emailAddress' => 'valid@example.com', + 'alias' => [ + 'Princess Peach', + 'Princess Peach', + ], + ]; + $this->assertTrue($this->validation->run($data)); + } + + public function testMatchesWithDotArrayFail(): void + { + $rules = [ + 'name' => 'permit_empty', + 'emailAddress' => 'permit_empty|valid_email', + 'alias.*' => 'permit_empty|matches[name]', + ]; + $this->validation->setRules($rules); + + $data = [ + 'name' => 'Princess Peach', + 'emailAddress' => 'valid@example.com', + 'alias' => [ + 'Princess ', + 'Princess Peach', + ], + ]; + $this->assertFalse($this->validation->run($data)); + $this->assertSame( + ['alias.0' => 'The alias.* field does not match the name field.'], + $this->validation->getErrors() + ); + } + public static function provideMatchesNestedCases(): iterable { yield from [ @@ -373,6 +417,50 @@ public function testDiffersNested(array $data, bool $expected): void $this->assertSame(! $expected, $this->validation->run($data)); } + public function testDiffersWithDotArrayPass(): void + { + $rules = [ + 'name' => 'permit_empty', + 'emailAddress' => 'permit_empty|valid_email', + 'alias.*' => 'permit_empty|differs[name]', + ]; + $this->validation->setRules($rules); + + $data = [ + 'name' => 'Princess Peach', + 'emailAddress' => 'valid@example.com', + 'alias' => [ + 'Princess Toadstool', + 'Peach', + ], + ]; + $this->assertTrue($this->validation->run($data)); + } + + public function testDiffersWithDotArrayFail(): void + { + $rules = [ + 'name' => 'permit_empty', + 'emailAddress' => 'permit_empty|valid_email', + 'alias.*' => 'permit_empty|differs[name]', + ]; + $this->validation->setRules($rules); + + $data = [ + 'name' => 'Princess Peach', + 'emailAddress' => 'valid@example.com', + 'alias' => [ + 'Princess Toadstool', + 'Princess Peach', + ], + ]; + $this->assertFalse($this->validation->run($data)); + $this->assertSame( + ['alias.1' => 'The alias.* field must differ from the name field.'], + $this->validation->getErrors() + ); + } + #[DataProvider('provideEquals')] public function testEquals(array $data, string $param, bool $expected): void { From 156e45bab1ed27523d995fc0a7a106e2087a0b26 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 3 Aug 2024 10:04:59 +0900 Subject: [PATCH 2/2] fix: rule `differs` and `matches` with dot array --- system/Validation/StrictRules/Rules.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/system/Validation/StrictRules/Rules.php b/system/Validation/StrictRules/Rules.php index e4fc4fc91142..ec02a4e0a0ac 100644 --- a/system/Validation/StrictRules/Rules.php +++ b/system/Validation/StrictRules/Rules.php @@ -48,11 +48,15 @@ public function differs( return $str !== dot_array_search($otherField, $data); } - if (! array_key_exists($field, $data)) { + if (! array_key_exists($otherField, $data)) { return false; } - if (! array_key_exists($otherField, $data)) { + if (str_contains($field, '.')) { + if (! ArrayHelper::dotKeyExists($field, $data)) { + return false; + } + } elseif (! array_key_exists($field, $data)) { return false; } @@ -281,11 +285,15 @@ public function matches( return $str === dot_array_search($otherField, $data); } - if (! array_key_exists($field, $data)) { + if (! array_key_exists($otherField, $data)) { return false; } - if (! array_key_exists($otherField, $data)) { + if (str_contains($field, '.')) { + if (! ArrayHelper::dotKeyExists($field, $data)) { + return false; + } + } elseif (! array_key_exists($field, $data)) { return false; }