From 8bd02502e5cb44f91964241a5bc93b762ca7557e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 26 Oct 2022 01:27:39 +0200 Subject: [PATCH] Fix UUID validation regexp MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/Utils.ts | 4 +++- test/utils/UtilsTest.ts | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 719200fa..bc957c24 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -16,7 +16,9 @@ export default class Utils { } public static validateUUID(uuid: string): boolean { - return /\/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$\/i/.test(uuid); + return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test( + uuid + ); } public static async sleep(milliSeconds: number): Promise { diff --git a/test/utils/UtilsTest.ts b/test/utils/UtilsTest.ts index c431642e..9e882cc5 100644 --- a/test/utils/UtilsTest.ts +++ b/test/utils/UtilsTest.ts @@ -3,6 +3,12 @@ import expect from 'expect'; import Utils from '../../src/utils/Utils'; describe('Utils test suite', () => { + it('Verify generateUUID()/validateUUID()', () => { + const uuid = Utils.generateUUID(); + expect(uuid.length).toEqual(36); + expect(Utils.validateUUID(uuid)).toBe(true); + }); + it('Verify secureRandom()', () => { const random = Utils.secureRandom(); expect(typeof random === 'number').toBe(true); -- 2.34.1