-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into chore/prepare-release
- Loading branch information
Showing
19 changed files
with
365 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export default {}; | ||
export * from "./principal"; | ||
export * from "./url"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Principal } from "@dfinity/principal"; | ||
import { PrincipalTextSchema } from "./principal"; | ||
|
||
describe("PrincipalText", () => { | ||
const mockPrincipalText = | ||
"xlmdg-vkosz-ceopx-7wtgu-g3xmd-koiyc-awqaq-7modz-zf6r6-364rh-oqe"; | ||
|
||
it("should pass validation with a valid Principal string", () => { | ||
const result = PrincipalTextSchema.safeParse(mockPrincipalText); | ||
expect(result.success).toBe(true); | ||
}); | ||
|
||
it("should fail validation with an invalid Principal string", () => { | ||
const invalidPrincipal = "invalid-principal"; | ||
const result = PrincipalTextSchema.safeParse(invalidPrincipal); | ||
expect(result.success).toBe(false); | ||
if (!result.success) { | ||
expect(result.error.errors[0].message).toBe( | ||
"Invalid textual representation of a Principal.", | ||
); | ||
} | ||
}); | ||
|
||
it("should pass validation with an anonymous Principal", () => { | ||
const validPrincipal = Principal.anonymous().toText(); | ||
const result = PrincipalTextSchema.safeParse(validPrincipal); | ||
expect(result.success).toBe(true); | ||
}); | ||
|
||
it("should fail validation with a non-string input", () => { | ||
const invalidPrincipal = 12345; | ||
const result = PrincipalTextSchema.safeParse(invalidPrincipal); | ||
expect(result.success).toBe(false); | ||
if (!result.success) { | ||
expect(result.error.errors[0].message).toBe( | ||
"Expected string, received number", | ||
); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Principal } from "@dfinity/principal"; | ||
import * as z from "zod"; | ||
|
||
/** | ||
* Zod schema to validate a string as a valid textual representation of a Principal. | ||
* | ||
* This schema checks if the provided string can be converted into a `Principal` instance. | ||
* If the conversion fails, validation will return an error message. | ||
* | ||
* @example | ||
* ```typescript | ||
* const result = PrincipalTextSchema.safeParse('aaaaa-aa'); | ||
* console.log(result.success); // true or false | ||
* ``` | ||
*/ | ||
export const PrincipalTextSchema = z.string().refine( | ||
(principal) => { | ||
try { | ||
Principal.fromText(principal); | ||
return true; | ||
} catch (err: unknown) { | ||
return false; | ||
} | ||
}, | ||
{ | ||
message: "Invalid textual representation of a Principal.", | ||
}, | ||
); | ||
|
||
export type PrincipalText = z.infer<typeof PrincipalTextSchema>; |
Oops, something went wrong.