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

Commit

Permalink
feat: define UuidStruct as a named type (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc authored Oct 23, 2023
1 parent 7417246 commit 09df0e2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
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;

/**
* 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

0 comments on commit 09df0e2

Please sign in to comment.