Skip to content

Commit e89fbd8

Browse files
committed
fix: strict validation rule matches and differs
1 parent 82382da commit e89fbd8

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

system/Validation/StrictRules/Rules.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,26 @@ public function __construct()
3636
* @param array|bool|float|int|object|string|null $str
3737
* @param array $data Other field/value pairs
3838
*/
39-
public function differs($str, string $field, array $data): bool
40-
{
41-
if (! is_string($str)) {
39+
public function differs(
40+
$str,
41+
string $otherField,
42+
array $data,
43+
?string $error = null,
44+
?string $field = null
45+
): bool {
46+
if (strpos($otherField, '.') !== false) {
47+
return $str !== dot_array_search($otherField, $data);
48+
}
49+
50+
if (! array_key_exists($field, $data)) {
4251
return false;
4352
}
4453

45-
return $this->nonStrictRules->differs($str, $field, $data);
54+
if (! array_key_exists($otherField, $data)) {
55+
return false;
56+
}
57+
58+
return $str !== ($data[$otherField] ?? null);
4659
}
4760

4861
/**
@@ -254,9 +267,26 @@ public function less_than_equal_to($str, string $max): bool
254267
* @param array|bool|float|int|object|string|null $str
255268
* @param array $data Other field/value pairs
256269
*/
257-
public function matches($str, string $field, array $data): bool
258-
{
259-
return $this->nonStrictRules->matches($str, $field, $data);
270+
public function matches(
271+
$str,
272+
string $otherField,
273+
array $data,
274+
?string $error = null,
275+
?string $field = null
276+
): bool {
277+
if (strpos($otherField, '.') !== false) {
278+
return $str === dot_array_search($otherField, $data);
279+
}
280+
281+
if (! array_key_exists($field, $data)) {
282+
return false;
283+
}
284+
285+
if (! array_key_exists($otherField, $data)) {
286+
return false;
287+
}
288+
289+
return $str === ($data[$otherField] ?? null);
260290
}
261291

262292
/**

tests/system/Validation/RulesTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,13 @@ public static function provideMatches(): iterable
303303
yield from [
304304
'foo bar not exist' => [[], false],
305305
'bar not exist' => [['foo' => null], false],
306-
'foo not exist' => [['bar' => null], true], // should be false?
306+
'foo not exist' => [['bar' => null], true], // Strict Rule: false
307307
'foo bar null' => [['foo' => null, 'bar' => null], true],
308308
'foo bar string match' => [['foo' => 'match', 'bar' => 'match'], true],
309309
'foo bar string not match' => [['foo' => 'match', 'bar' => 'nope'], false],
310-
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], false], // should be true
310+
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], false], // Strict Rule: true
311311
'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], false],
312-
'foo bar bool match' => [['foo' => true, 'bar' => true], false], // should be true
312+
'foo bar bool match' => [['foo' => true, 'bar' => true], false], // Strict Rule: true
313313
];
314314
}
315315

@@ -348,9 +348,9 @@ public static function provideDiffers(): iterable
348348
'foo bar null' => [['foo' => null, 'bar' => null], false],
349349
'foo bar string match' => [['foo' => 'match', 'bar' => 'match'], false],
350350
'foo bar string not match' => [['foo' => 'match', 'bar' => 'nope'], true],
351-
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], true], // should be false
351+
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], true], // Strict Rule: false
352352
'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], true],
353-
'foo bar bool match' => [['foo' => true, 'bar' => true], true], // should be false
353+
'foo bar bool match' => [['foo' => true, 'bar' => true], true], // Strict Rule: false
354354
];
355355
}
356356

tests/system/Validation/StrictRules/RulesTest.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,13 @@ public static function provideMatches(): iterable
213213
yield from [
214214
'foo bar not exist' => [[], false],
215215
'bar not exist' => [['foo' => null], false],
216-
'foo not exist' => [['bar' => null], true],
216+
'foo not exist' => [['bar' => null], false],
217217
'foo bar null' => [['foo' => null, 'bar' => null], true],
218218
'foo bar string match' => [['foo' => 'match', 'bar' => 'match'], true],
219219
'foo bar string not match' => [['foo' => 'match', 'bar' => 'nope'], false],
220-
// TypeError: CodeIgniter\Validation\Rules::matches(): Argument #1 ($str) must be of type ?string, float given
221-
// 'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], true],
222-
// TypeError: CodeIgniter\Validation\Rules::matches(): Argument #1 ($str) must be of type ?string, float given
223-
// 'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], false],
224-
// TypeError: CodeIgniter\Validation\Rules::matches(): Argument #1 ($str) must be of type ?string, float given
225-
// 'foo bar bool match' => [['foo' => true, 'bar' => true], true],
220+
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], true],
221+
'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], false],
222+
'foo bar bool match' => [['foo' => true, 'bar' => true], true],
226223
];
227224
}
228225

@@ -245,7 +242,7 @@ public static function provideDiffers(): iterable
245242
'foo bar string match' => [['foo' => 'match', 'bar' => 'match'], false],
246243
'foo bar string not match' => [['foo' => 'match', 'bar' => 'nope'], true],
247244
'foo bar float match' => [['foo' => 1.2, 'bar' => 1.2], false],
248-
'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], false],
245+
'foo bar float not match' => [['foo' => 1.2, 'bar' => 2.3], true],
249246
'foo bar bool match' => [['foo' => true, 'bar' => true], false],
250247
];
251248
}

0 commit comments

Comments
 (0)