Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Validation rule differs/matches with dot array #9103

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' => '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 [
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' => '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
{
Expand Down
Loading