Skip to content

Commit

Permalink
Merge pull request #9103 from kenjis/fix-validation-differs-matches
Browse files Browse the repository at this point in the history
fix: Validation rule `differs`/`matches` with dot array
  • Loading branch information
kenjis authored Aug 6, 2024
2 parents 0e96b3c + 156e45b commit 5a340d0
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 4 deletions.
16 changes: 12 additions & 4 deletions system/Validation/StrictRules/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
88 changes: 88 additions & 0 deletions tests/system/Validation/RulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]',
'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' => '[email protected]',
'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 [
Expand Down Expand Up @@ -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' => '[email protected]',
'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' => '[email protected]',
'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
{
Expand Down

0 comments on commit 5a340d0

Please sign in to comment.