Skip to content

Commit

Permalink
fix(isUUID) for null version argument supply
Browse files Browse the repository at this point in the history
  • Loading branch information
theteladras committed Oct 12, 2021
1 parent 13651ea commit 1620e82
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/lib/isUUID.js
Expand Up @@ -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);
}
18 changes: 18 additions & 0 deletions test/validators.js
Expand Up @@ -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],
Expand Down

0 comments on commit 1620e82

Please sign in to comment.