From 86151b4cbc0761b7a9529ddd8c5bb31041f2a52a Mon Sep 17 00:00:00 2001 From: Jeff Reiffers Date: Wed, 11 Oct 2023 08:50:17 +0200 Subject: [PATCH] fix: loosen uuid validation and support uuid v8 (custom) --- libs/utils/src/lib/validation/uuid.spec.ts | 11 +++++++++++ libs/utils/src/lib/validation/uuid.ts | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 libs/utils/src/lib/validation/uuid.spec.ts diff --git a/libs/utils/src/lib/validation/uuid.spec.ts b/libs/utils/src/lib/validation/uuid.spec.ts new file mode 100644 index 000000000..a0f00cc23 --- /dev/null +++ b/libs/utils/src/lib/validation/uuid.spec.ts @@ -0,0 +1,11 @@ +import { validUUID } from './uuid'; + +describe('validUUID', () => { + test('validate valid uuid', () => { + expect(validUUID('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b')).toStrictEqual(true); + expect(validUUID('596547fb-f496-804e-933c-1fc948dd6ada')).toStrictEqual(true); + }); + test('validate invalid uuid', () => { + expect(validUUID('invalid-uuid')).toStrictEqual(false); + }); +}); diff --git a/libs/utils/src/lib/validation/uuid.ts b/libs/utils/src/lib/validation/uuid.ts index c2c564d54..6cf744a42 100644 --- a/libs/utils/src/lib/validation/uuid.ts +++ b/libs/utils/src/lib/validation/uuid.ts @@ -1,3 +1,3 @@ export const validUUID = (uuid?: string) => { - return uuid?.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i) !== null; + return uuid?.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{3,4}-[0-9a-f]{3,4}-[0-9a-f]{12}$/i) !== null; };