Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

feat: define UuidStruct as a named type #159

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/rpc-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('keyringRpcDispatcher', () => {
};

await expect(handleKeyringRequest(keyring, request)).rejects.toThrow(
'At path: params.id -- Expected a string, but received: undefined',
'At path: params.id -- Expected a value of type `UuidV4`, but received: `undefined`',
);
});

Expand Down
11 changes: 7 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { assert, pattern, string } from 'superstruct';
import { assert, define } from 'superstruct';
import type { Struct } from 'superstruct';

const UUID_V4_REGEX =
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu;

gantunesr marked this conversation as resolved.
Show resolved Hide resolved
/**
* UUIDv4 struct.
*/
export const UuidStruct = pattern(
string(),
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu,
export const UuidStruct = define<string>(
'UuidV4',
(id: unknown): boolean => typeof id === 'string' && UUID_V4_REGEX.test(id),
);

/**
Expand Down