mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 15:53:27 +00:00
Introduce a stricter use of the OrganizationId type on org-vault exports (#15836)
Co-authored-by: Daniel James Smith <djsmith85@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
68d7cb4846
commit
b091719748
35
libs/common/src/types/guid.spec.ts
Normal file
35
libs/common/src/types/guid.spec.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { isId, emptyGuid, OrganizationId } from "./guid";
|
||||
|
||||
describe("isId tests", () => {
|
||||
it("should return true for a valid guid string", () => {
|
||||
// Example valid GUID
|
||||
const validGuid = "12345678-1234-1234-1234-123456789abc";
|
||||
expect(isId(validGuid)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false for an invalid guid string", () => {
|
||||
// Example invalid GUID
|
||||
const invalidGuid = "not-a-guid";
|
||||
expect(isId(invalidGuid)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for non-string values", () => {
|
||||
expect(isId(undefined)).toBe(false);
|
||||
expect(isId(null)).toBe(false);
|
||||
expect(isId(123)).toBe(false);
|
||||
expect(isId({})).toBe(false);
|
||||
expect(isId([])).toBe(false);
|
||||
});
|
||||
|
||||
it("should return true for the emptyGuid constant if it is a valid guid", () => {
|
||||
expect(isId(emptyGuid)).toBe(true);
|
||||
});
|
||||
|
||||
it("should infer type OrganizationId when using isId<OrganizationId>", () => {
|
||||
const orgId: string = "12345678-1234-1234-1234-123456789abc";
|
||||
if (isId<OrganizationId>(orgId)) {
|
||||
return;
|
||||
}
|
||||
throw new Error("Type guard failed, orgId is not a valid OrganizationId");
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,7 @@
|
||||
import { Opaque } from "type-fest";
|
||||
|
||||
import { isGuid } from "@bitwarden/guid";
|
||||
|
||||
export type Guid = Opaque<string, "Guid">;
|
||||
|
||||
// Convenience re-export of UserId from it's original location, any library that
|
||||
@@ -26,3 +28,23 @@ export type OrganizationReportId = Opaque<string, "OrganizationReportId">;
|
||||
* A string representation of an empty guid.
|
||||
*/
|
||||
export const emptyGuid = "00000000-0000-0000-0000-000000000000";
|
||||
|
||||
/**
|
||||
* Determines if the provided value is a valid GUID string.
|
||||
*
|
||||
* @typeParam SomeGuid - The input type, defaults to `Guid`.
|
||||
* @typeParam Output - The output type, resolves to `SomeGuid` if it is an opaque string, otherwise to `Guid` if `SomeGuid` is a string, or `never`.
|
||||
* @param id - The value to check.
|
||||
* @returns `true` if `id` is a string and a valid GUID, otherwise `false`.
|
||||
*/
|
||||
export function isId<
|
||||
SomeGuid extends string = Guid,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
Output = SomeGuid extends Opaque<string, infer T>
|
||||
? SomeGuid
|
||||
: SomeGuid extends string
|
||||
? Guid
|
||||
: never,
|
||||
>(id: unknown): id is Output {
|
||||
return typeof id === "string" && isGuid(id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user