Skip to content

Commit

Permalink
Merge branch 'main' into chore/prepare-release
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored Jan 20, 2025
2 parents eeb53d6 + 8848c69 commit 3ab7833
Show file tree
Hide file tree
Showing 19 changed files with 365 additions and 13 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2021 DFINITY Stiftung.
Copyright 2025 DFINITY Stiftung.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@
"@dfinity/principal"
]
},
{
"name": "@dfinity/zod-schemas",
"path": "./packages/zod-schemas/dist/index.js",
"limit": "2 kB",
"gzip": true,
"ignore": [
"zod",
"@dfinity/principal"
]
},
{
"name": "@dfinity/ic-management",
"path": "./packages/ic-management/dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/ckbtc/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2021 DFINITY Stiftung.
Copyright 2025 DFINITY Stiftung.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion packages/cketh/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2021 DFINITY Stiftung.
Copyright 2025 DFINITY Stiftung.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion packages/cmc/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2021 DFINITY Stiftung.
Copyright 2025 DFINITY Stiftung.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion packages/ic-management/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2021 DFINITY Stiftung.
Copyright 2025 DFINITY Stiftung.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion packages/ledger-icp/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2021 DFINITY Stiftung.
Copyright 2025 DFINITY Stiftung.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion packages/ledger-icrc/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2021 DFINITY Stiftung.
Copyright 2025 DFINITY Stiftung.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion packages/nns-proto/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2021 DFINITY Stiftung.
Copyright 2025 DFINITY Stiftung.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion packages/nns/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2021 DFINITY Stiftung.
Copyright 2025 DFINITY Stiftung.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion packages/sns/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2021 DFINITY Stiftung.
Copyright 2025 DFINITY Stiftung.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2021 DFINITY Stiftung.
Copyright 2025 DFINITY Stiftung.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion packages/zod-schemas/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2021 DFINITY Stiftung.
Copyright 2025 DFINITY Stiftung.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
71 changes: 71 additions & 0 deletions packages/zod-schemas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,75 @@ npm i @dfinity/principal

<!-- TSDOC_START -->

### :toolbox: Functions

- [createUrlSchema](#gear-createurlschema)

#### :gear: createUrlSchema

Creates a Zod schema for validating URLs. By default, it validates that the URL protocol is HTTPS and allow usage of HTTP only locally.

| Function | Type |
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `createUrlSchema` | `({ additionalProtocols, allowHttpLocally, }: { additionalProtocols?: `${string}:`[] or undefined; allowHttpLocally?: boolean or undefined; }) => ZodEffects<ZodString, string, string>` |

Parameters:

- `options`: - Configuration options for the schema.
- `options.additionalProtocols`: - Additional protocols to allow (e.g., "wss:" or "ftp:"). ⚠️ Usage of insecure protocols is discouraged.
- `options.allowHttpLocally`: - Whether to allow HTTP for localhost and 127.0.0.1. Default: true.

Examples:

const schema = createUrlSchema({
additionalProtocols: ["wss:"],
allowHttpLocally: false
});

schema.parse("https://example.com"); // Valid
schema.parse("wss://example.com"); // Valid
schema.parse("http://localhost"); // Invalid if allowHttpLocally is false

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/zod-schemas/src/url.ts#L27)

### :wrench: Constants

- [PrincipalTextSchema](#gear-principaltextschema)
- [UrlSchema](#gear-urlschema)

#### :gear: PrincipalTextSchema

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.

| Constant | Type |
| --------------------- | --------------------------------------- |
| `PrincipalTextSchema` | `ZodEffects<ZodString, string, string>` |

Examples:

```typescript
const result = PrincipalTextSchema.safeParse("aaaaa-aa");
console.log(result.success); // true or false
```

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/zod-schemas/src/principal.ts#L16)

#### :gear: UrlSchema

Default URL schema that enforces HTTPS and allows HTTP locally.

| Constant | Type |
| ----------- | --------------------------------------- |
| `UrlSchema` | `ZodEffects<ZodString, string, string>` |

Examples:

UrlSchema.parse("https://example.com"); // Valid
UrlSchema.parse("http://127.0.0.1"); // Valid (localhost exception)

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/zod-schemas/src/url.ts#L70)

<!-- TSDOC_END -->
3 changes: 2 additions & 1 deletion packages/zod-schemas/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export default {};
export * from "./principal";
export * from "./url";
40 changes: 40 additions & 0 deletions packages/zod-schemas/src/principal.spec.ts
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",
);
}
});
});
30 changes: 30 additions & 0 deletions packages/zod-schemas/src/principal.ts
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>;
Loading

0 comments on commit 3ab7833

Please sign in to comment.