Skip to content

Commit

Permalink
fix: traditional validation rule matches and differs
Browse files Browse the repository at this point in the history
Match CI3 behaviros.
  • Loading branch information
kenjis committed Oct 29, 2023
1 parent e89fbd8 commit 36c0de9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
12 changes: 7 additions & 5 deletions system/Validation/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ class Rules
/**
* The value does not match another field in $data.
*
* @param array $data Other field/value pairs
* @param string|null $str
* @param array $data Other field/value pairs
*/
public function differs(?string $str, string $field, array $data): bool
public function differs($str, string $field, array $data): bool
{
if (strpos($field, '.') !== false) {
return $str !== dot_array_search($field, $data);
Expand Down Expand Up @@ -177,15 +178,16 @@ public function less_than_equal_to(?string $str, string $max): bool
/**
* Matches the value of another field in $data.
*
* @param array $data Other field/value pairs
* @param string|null $str
* @param array $data Other field/value pairs
*/
public function matches(?string $str, string $field, array $data): bool
public function matches($str, string $field, array $data): bool
{
if (strpos($field, '.') !== false) {
return $str === dot_array_search($field, $data);
}

return array_key_exists($field, $data) && $str === $data[$field];
return isset($data[$field]) ? ($str === $data[$field]) : false;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/system/Validation/RulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,13 @@ public static function provideMatches(): iterable
yield from [
'foo bar not exist' => [[], false],
'bar not exist' => [['foo' => null], false],
'foo not exist' => [['bar' => null], true], // Strict Rule: false
'foo bar null' => [['foo' => null, 'bar' => null], true],
'foo not exist' => [['bar' => null], false],
'foo bar null' => [['foo' => null, 'bar' => null], false], // Strict Rule: true
'foo bar string match' => [['foo' => 'match', 'bar' => 'match'], true],
'foo bar string not match' => [['foo' => 'match', 'bar' => 'nope'], false],
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], false], // Strict Rule: true
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], true],
'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], false],
'foo bar bool match' => [['foo' => true, 'bar' => true], false], // Strict Rule: true
'foo bar bool match' => [['foo' => true, 'bar' => true], true],
];
}

Expand Down Expand Up @@ -348,9 +348,9 @@ public static function provideDiffers(): iterable
'foo bar null' => [['foo' => null, 'bar' => null], false],
'foo bar string match' => [['foo' => 'match', 'bar' => 'match'], false],
'foo bar string not match' => [['foo' => 'match', 'bar' => 'nope'], true],
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], true], // Strict Rule: false
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], false],
'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], true],
'foo bar bool match' => [['foo' => true, 'bar' => true], true], // Strict Rule: false
'foo bar bool match' => [['foo' => true, 'bar' => true], false],
];
}

Expand Down

0 comments on commit 36c0de9

Please sign in to comment.