From 0188ed5562510d1d35f051f7991622546b5db238 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 24 Jan 2023 23:26:55 +0100 Subject: [PATCH] Fixed StringInputLength validation --- src/Http/Validation/Rules/StringInputLength.php | 14 ++++++++------ .../Validation/Rules/StringInputLengthTest.php | 6 ++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Http/Validation/Rules/StringInputLength.php b/src/Http/Validation/Rules/StringInputLength.php index 3b23fcd6..23695f34 100644 --- a/src/Http/Validation/Rules/StringInputLength.php +++ b/src/Http/Validation/Rules/StringInputLength.php @@ -2,9 +2,9 @@ namespace Engelsystem\Http\Validation\Rules; -use Engelsystem\Helpers\Carbon; +use DateTime; +use Exception; use Illuminate\Support\Str; -use Throwable; trait StringInputLength { @@ -27,11 +27,13 @@ trait StringInputLength protected function isDateTime(mixed $input): bool { try { - $date = Carbon::make($input); - } catch (Throwable $e) { - return false; + $input = new DateTime($input); + // Min 1s diff to exclude any not auto-detected dates / times like ... + return abs((new DateTime())->diff($input)->format('%s')) > 1; + } catch (Exception $e) { + // Ignore it } - return !is_null($date); + return false; } } diff --git a/tests/Unit/Http/Validation/Rules/StringInputLengthTest.php b/tests/Unit/Http/Validation/Rules/StringInputLengthTest.php index 0a2b2c22..78bde4d9 100644 --- a/tests/Unit/Http/Validation/Rules/StringInputLengthTest.php +++ b/tests/Unit/Http/Validation/Rules/StringInputLengthTest.php @@ -28,6 +28,7 @@ class StringInputLengthTest extends TestCase return [ ['TEST', 4], ['?', 1], + ['', 0], ['2042-01-01 00:00', '2042-01-01 00:00'], ['2042-01-01', '2042-01-01'], ['12:42', '12:42'], @@ -35,6 +36,11 @@ class StringInputLengthTest extends TestCase ['...', 3], ['Test Tester', 11], ['com', 3], + ['Test', 4], + ['H', 1], + ['3', 3], + [42, 42], + [99.3, 99.3], ]; } }