From 47e44c15cc8292e1c4320b06aa53361593cf0992 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 2 Dec 2024 15:36:21 +0900 Subject: [PATCH] Add a uuid4 validate method --- .../function/upgrade_serial_to_identity.sql | 6 ++-- 4dev/tests/Create/CoreLibsCreateUidsTest.php | 18 ++++++---- www/admin/class_test.uids.php | 8 +++++ www/lib/CoreLibs/Create/Uids.php | 34 ++++++++----------- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/4dev/database/function/upgrade_serial_to_identity.sql b/4dev/database/function/upgrade_serial_to_identity.sql index 77585e3b..2c2ac212 100644 --- a/4dev/database/function/upgrade_serial_to_identity.sql +++ b/4dev/database/function/upgrade_serial_to_identity.sql @@ -1,6 +1,8 @@ --- Upgrae serial to identity type +-- Upgrade serial to identity type -- --- @param reclass tbl The table where the column is located +-- Original: https://www.enterprisedb.com/blog/postgresql-10-identity-columns-explained#section-6 +-- +-- @param reclass tbl The table where the column is located, prefix with 'schema.' if different schema -- @param name col The column to be changed -- @param varchar identity_type [default=a] Allowed a, d, assigned, default -- @param varchar col_type [default=''] Allowed smallint, int, bigint, int2, int4, int8 diff --git a/4dev/tests/Create/CoreLibsCreateUidsTest.php b/4dev/tests/Create/CoreLibsCreateUidsTest.php index 3612ee89..308d9640 100644 --- a/4dev/tests/Create/CoreLibsCreateUidsTest.php +++ b/4dev/tests/Create/CoreLibsCreateUidsTest.php @@ -121,6 +121,7 @@ final class CoreLibsCreateUidsTest extends TestCase * must match 7e78fe0d-59b8-4637-af7f-e88d221a7d1e * * @covers ::uuidv4 + * @covers ::validateUuidv4 * @testdox uuidv4 check that return is matching regex [$_dataName] * * @return void @@ -129,13 +130,18 @@ final class CoreLibsCreateUidsTest extends TestCase { $uuid = \CoreLibs\Create\Uids::uuidv4(); $this->assertMatchesRegularExpression( - '/^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/', - $uuid + '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/', + $uuid, + 'Failed regex check' + ); + $this->assertTrue( + \CoreLibs\Create\Uids::validateUuuidv4($uuid), + 'Failed validate regex method' + ); + $this->assertFalse( + \CoreLibs\Create\Uids::validateUuuidv4('not-a-uuidv4'), + 'Failed wrong uuid validated as true' ); - // $this->assertStringMatchesFormat( - // '%4s%4s-%4s-%4s-%4s-%4s%4s%4s', - // $uuid - // ); } /** diff --git a/www/admin/class_test.uids.php b/www/admin/class_test.uids.php index 3801eaed..3302fe8b 100644 --- a/www/admin/class_test.uids.php +++ b/www/admin/class_test.uids.php @@ -52,6 +52,14 @@ print "S:UNIQID (512): " . Uids::uniqId(512) . "
"; // uniq ids print "UNIQU ID SHORT : " . Uids::uniqIdShort() . "
"; print "UNIQU ID LONG : " . Uids::uniqIdLong() . "
"; +// validate +$uuidv4 = Uids::uuidv4(); +if (!Uids::validateUuuidv4($uuidv4)) { + print "Invalid UUIDv4: " . $uuidv4 . "
"; +} +if (!Uids::validateUuuidv4("foobar")) { + print "Invalid UUIDv4: hard coded
"; +} // DEPRECATED /* print "D/UUIDV4: ".$basic->uuidv4()."
"; diff --git a/www/lib/CoreLibs/Create/Uids.php b/www/lib/CoreLibs/Create/Uids.php index 42339797..09bdc86b 100644 --- a/www/lib/CoreLibs/Create/Uids.php +++ b/www/lib/CoreLibs/Create/Uids.php @@ -56,26 +56,6 @@ class Uids */ public static function uuidv4(): string { - /* return sprintf( - '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', - // 32 bits for "time_low" - mt_rand(0, 0xffff), - mt_rand(0, 0xffff), - // 16 bits for "time_mid" - mt_rand(0, 0xffff), - // 16 bits for "time_hi_and_version", - // four most significant bits holds version number 4 - mt_rand(0, 0x0fff) | 0x4000, - // 16 bits, 8 bits for "clk_seq_hi_res", - // 8 bits for "clk_seq_low", - // two most significant bits holds zero and one for variant DCE1.1 - mt_rand(0, 0x3fff) | 0x8000, - // 48 bits for "node" - mt_rand(0, 0xffff), - mt_rand(0, 0xffff), - mt_rand(0, 0xffff) - ); */ - $data = random_bytes(16); assert(strlen($data) == 16); @@ -93,6 +73,20 @@ class Uids return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); } + /** + * regex validate uuid v4 + * + * @param string $uuidv4 + * @return bool + */ + public static function validateUuuidv4(string $uuidv4): bool + { + if (!preg_match("/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/", $uuidv4)) { + return false; + } + return true; + } + /** * creates a uniq id based on lengths *