From 1620e82414a8f703e00010f472a39e0b08b86372 Mon Sep 17 00:00:00 2001 From: Sanel Hadzini Date: Tue, 12 Oct 2021 14:38:08 +0200 Subject: [PATCH] fix(isUUID) for null version argument supply --- src/lib/isUUID.js | 11 +++++++++-- test/validators.js | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/lib/isUUID.js b/src/lib/isUUID.js index 61d938ac3..0174ae745 100644 --- a/src/lib/isUUID.js +++ b/src/lib/isUUID.js @@ -7,8 +7,15 @@ const uuid = { all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i, }; -export default function isUUID(str, version = 'all') { +function extractUUIDVersion(version) { + const isKnownVersion = Object.keys(uuid).includes(String(version)); + if (isKnownVersion) return version; + return version ? undefined : 'all'; +} + +export default function isUUID(str, version) { assertString(str); - const pattern = uuid[version]; + const uuidVersion = extractUUIDVersion(version); + const pattern = uuid[uuidVersion]; return pattern && pattern.test(str); } diff --git a/test/validators.js b/test/validators.js index 91794d521..12db2030f 100644 --- a/test/validators.js +++ b/test/validators.js @@ -4413,6 +4413,24 @@ describe('Validators', () => { 'AAAAAAAA-1111-1111-AAAG-111111111111', ], }); + test({ + validator: 'isUUID', + args: [null], + valid: [ + 'A117FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A117FBC9-4BED-4078-8F07-9141BA07C9F3', + 'A117FBC9-4BED-5078-AF07-9141BA07C9F3', + ], + invalid: [ + '', + 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A117FBC9-4BED-3078-CF07-9141BA07C9F3xxx', + 'A117FBC94BED3078CF079141BA07C9F3', + '911859', + '911FBC9-4BED-3078-CF07A-9141BA07C9F3', + 'A11AAAAA-1111-1111-AAAG-111111111111', + ], + }); test({ validator: 'isUUID', args: [3],