From 837cdb5449d721cc6b0ffda2d5abb7154b27fe52 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 | 7 ++++--- test/validators.js | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/lib/isUUID.js b/src/lib/isUUID.js index 61d938ac3..0cb54fbcd 100644 --- a/src/lib/isUUID.js +++ b/src/lib/isUUID.js @@ -7,8 +7,9 @@ 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') { +export default function isUUID(str, version) { assertString(str); - const pattern = uuid[version]; - return pattern && pattern.test(str); + if (version && !Object.keys(uuid).includes(String(version))) return false; + const pattern = uuid[version || 'all']; + return pattern.test(str); } diff --git a/test/validators.js b/test/validators.js index 91794d521..6f80eec9b 100644 --- a/test/validators.js +++ b/test/validators.js @@ -4398,6 +4398,25 @@ describe('Validators', () => { it('should validate UUIDs', () => { test({ validator: 'isUUID', + args: [undefined], + valid: [ + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-4078-8F07-9141BA07C9F3', + 'A987FBC9-4BED-5078-AF07-9141BA07C9F3', + ], + invalid: [ + '', + 'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3', + 'A987FBC9-4BED-3078-CF07-9141BA07C9F3xxx', + 'A987FBC94BED3078CF079141BA07C9F3', + '934859', + '987FBC9-4BED-3078-CF07A-9141BA07C9F3', + 'AAAAAAAA-1111-1111-AAAG-111111111111', + ], + }); + test({ + validator: 'isUUID', + args: [null], valid: [ 'A987FBC9-4BED-3078-CF07-9141BA07C9F3', 'A987FBC9-4BED-4078-8F07-9141BA07C9F3',