From 26a3e75fe19bce3340d2f44cfdbfa7be61cdebf8 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 24 Oct 2023 18:58:12 +0900 Subject: [PATCH 1/2] test: add tests for exact_length --- tests/system/Validation/RulesTest.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/system/Validation/RulesTest.php b/tests/system/Validation/RulesTest.php index f031a129a286..23358d08904b 100644 --- a/tests/system/Validation/RulesTest.php +++ b/tests/system/Validation/RulesTest.php @@ -398,8 +398,10 @@ public function testMaxLengthReturnsFalseWithNonNumericVal(): void /** * @dataProvider provideExactLength + * + * @param int|string|null $data */ - public function testExactLength(?string $data, bool $expected): void + public function testExactLength($data, bool $expected): void { $this->validation->setRules(['foo' => 'exact_length[3]']); $this->assertSame($expected, $this->validation->run(['foo' => $data])); @@ -408,10 +410,13 @@ public function testExactLength(?string $data, bool $expected): void public static function provideExactLength(): iterable { yield from [ - 'null' => [null, false], - 'exact' => ['bar', true], - 'less' => ['ba', false], - 'greater' => ['bars', false], + 'null' => [null, false], + 'exact' => ['bar', true], + 'exact_int' => [123, true], + 'less' => ['ba', false], + 'less_int' => [12, false], + 'greater' => ['bars', false], + 'greater_int' => [1234, false], ]; } From 391723b1c8ffc9ac56cd0fd174a2e56f85413ce5 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 24 Oct 2023 18:58:42 +0900 Subject: [PATCH 2/2] fix: exact_length does not validate int values --- system/Validation/StrictRules/Rules.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/system/Validation/StrictRules/Rules.php b/system/Validation/StrictRules/Rules.php index 7043646c1f58..0887eff28ada 100644 --- a/system/Validation/StrictRules/Rules.php +++ b/system/Validation/StrictRules/Rules.php @@ -63,6 +63,10 @@ public function equals($str, string $val): bool */ public function exact_length($str, string $val): bool { + if (is_int($str) || is_float($str)) { + $str = (string) $str; + } + if (! is_string($str)) { return false; }