Skip to content

Commit

Permalink
feat: isValidUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
solaris007 committed Jan 9, 2025
1 parent 5e70a51 commit f85ac7a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
34 changes: 31 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/spacecat-shared-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@aws-sdk/client-secrets-manager": "3.721.0",
"@aws-sdk/client-sqs": "3.721.0",
"@json2csv/plainjs": "7.0.6",
"aws-xray-sdk": "3.10.2"
"aws-xray-sdk": "3.10.2",
"uuid": "11.0.4"
}
}
12 changes: 12 additions & 0 deletions packages/spacecat-shared-utils/src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* governing permissions and limitations under the License.
*/

import { validate as uuidValidate } from 'uuid';

// Precompile regular expressions
const REGEX_ISO_DATE = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/;
const REGEX_TIME_OFFSET_DATE = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}(Z|[+-]\d{2}:\d{2})/;
Expand Down Expand Up @@ -194,6 +196,15 @@ function isValidUrl(urlString) {
}
}

/**
* Validates whether the given string is a valid UUID.
* @param {string} uuid - The string to validate.
* @return {boolean} True if the given string is a valid UUID.
*/
function isValidUUID(uuid) {
return uuidValidate(uuid);
}

/**
* Converts a given value to a boolean. Throws an error if the value is not a boolean.
*
Expand Down Expand Up @@ -257,5 +268,6 @@ export {
isString,
isValidDate,
isValidUrl,
isValidUUID,
toBoolean,
};
2 changes: 2 additions & 0 deletions packages/spacecat-shared-utils/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export function toBoolean(value: unknown): boolean;

export function isValidUrl(urlString: string): boolean;

export function isValidUUID(uuid: string): boolean;

export function dateAfterDays(days: number, dateString: string): Date;

export function deepEqual(a: unknown, b: unknown): boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/spacecat-shared-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
isString,
isValidDate,
isValidUrl,
isValidUUID,
toBoolean,
} from './functions.js';

Expand Down
20 changes: 20 additions & 0 deletions packages/spacecat-shared-utils/test/functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
isString,
isValidDate,
isValidUrl,
isValidUUID,
toBoolean,
} from '../src/index.js';

Expand Down Expand Up @@ -320,6 +321,25 @@ describe('Shared functions', () => {
});
});

describe('isValidUUID', () => {
it('returns false for invalid UUID', async () => {
const invalidUUIDs = [
null,
undefined,
1234,
true,
'invalid uuid',
'123e4567-e89b-12d3-a456-42661417',
];

invalidUUIDs.forEach((uuid) => expect(isValidUUID(uuid)).to.be.false);
});

it('returns true for valid UUID', async () => {
expect(isValidUUID('123e4567-e89b-12d3-a456-426614174000')).to.be.true;
});
});

describe('dateAfterDays', () => {
const sandbox = sinon.createSandbox();

Expand Down
1 change: 1 addition & 0 deletions packages/spacecat-shared-utils/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('Index Exports', () => {
'isString',
'isValidDate',
'isValidUrl',
'isValidUUID',
'logWrapper',
'prependSchema',
'resolveCustomerSecretsName',
Expand Down

0 comments on commit f85ac7a

Please sign in to comment.