Fixed StringInputLength validation

This commit is contained in:
Igor Scheller 2023-01-24 23:26:55 +01:00
parent b99e6ed8c1
commit 0188ed5562
2 changed files with 14 additions and 6 deletions

View File

@ -2,9 +2,9 @@
namespace Engelsystem\Http\Validation\Rules; namespace Engelsystem\Http\Validation\Rules;
use Engelsystem\Helpers\Carbon; use DateTime;
use Exception;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Throwable;
trait StringInputLength trait StringInputLength
{ {
@ -27,11 +27,13 @@ trait StringInputLength
protected function isDateTime(mixed $input): bool protected function isDateTime(mixed $input): bool
{ {
try { try {
$date = Carbon::make($input); $input = new DateTime($input);
} catch (Throwable $e) { // Min 1s diff to exclude any not auto-detected dates / times like ...
return false; return abs((new DateTime())->diff($input)->format('%s')) > 1;
} catch (Exception $e) {
// Ignore it
} }
return !is_null($date); return false;
} }
} }

View File

@ -28,6 +28,7 @@ class StringInputLengthTest extends TestCase
return [ return [
['TEST', 4], ['TEST', 4],
['?', 1], ['?', 1],
['', 0],
['2042-01-01 00:00', '2042-01-01 00:00'], ['2042-01-01 00:00', '2042-01-01 00:00'],
['2042-01-01', '2042-01-01'], ['2042-01-01', '2042-01-01'],
['12:42', '12:42'], ['12:42', '12:42'],
@ -35,6 +36,11 @@ class StringInputLengthTest extends TestCase
['...', 3], ['...', 3],
['Test Tester', 11], ['Test Tester', 11],
['com', 3], ['com', 3],
['Test', 4],
['H', 1],
['3', 3],
[42, 42],
[99.3, 99.3],
]; ];
} }
} }