@dfinity/zod-schemas 3.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -62,13 +62,32 @@ schema.parse("https://example.com"); // Valid
62
62
  schema.parse("wss://example.com"); // Valid
63
63
  schema.parse("http://localhost"); // Invalid if allowHttpLocally is false
64
64
 
65
- [:link: Source](https://github.com/dfinity/icp-js-canisters/tree/main/packages/zod-schemas/src/url.ts#L27)
65
+ [:link: Source](https://github.com/dfinity/icp-js-canisters/tree/main/packages/zod-schemas/src/url.ts#L28)
66
66
 
67
67
  ### :wrench: Constants
68
68
 
69
+ - [Uint8ArraySchema](#gear-uint8arrayschema)
69
70
  - [PrincipalTextSchema](#gear-principaltextschema)
71
+ - [PrincipalSchema](#gear-principalschema)
70
72
  - [UrlSchema](#gear-urlschema)
71
73
 
74
+ #### :gear: Uint8ArraySchema
75
+
76
+ Zod schema to validate a value is a `Uint8Array` instance.
77
+
78
+ | Constant | Type |
79
+ | ------------------ | ------------------------------------------------------------- |
80
+ | `Uint8ArraySchema` | `ZodCustom<Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>>` |
81
+
82
+ Examples:
83
+
84
+ ```typescript
85
+ const result = Uint8ArraySchema.safeParse(new Uint8Array([1, 2, 3]));
86
+ console.log(result.success); // true or false
87
+ ```
88
+
89
+ [:link: Source](https://github.com/dfinity/icp-js-canisters/tree/main/packages/zod-schemas/src/arrays.ts#L13)
90
+
72
91
  #### :gear: PrincipalTextSchema
73
92
 
74
93
  Zod schema to validate a string as a valid textual representation of a Principal.
@@ -87,7 +106,27 @@ const result = PrincipalTextSchema.safeParse("aaaaa-aa");
87
106
  console.log(result.success); // true or false
88
107
  ```
89
108
 
90
- [:link: Source](https://github.com/dfinity/icp-js-canisters/tree/main/packages/zod-schemas/src/principal.ts#L16)
109
+ [:link: Source](https://github.com/dfinity/icp-js-canisters/tree/main/packages/zod-schemas/src/principal.ts#L17)
110
+
111
+ #### :gear: PrincipalSchema
112
+
113
+ Zod schema to validate and transform a value into a `Principal` instance.
114
+
115
+ This schema checks if the provided value is an instance or an object representing
116
+ a `Principal` and transforms it into a valid `Principal` instance.
117
+
118
+ | Constant | Type |
119
+ | ----------------- | ------------------------------------------------------------------------ |
120
+ | `PrincipalSchema` | `ZodPipe<ZodCustom<Principal, Principal>, ZodTransform<any, Principal>>` |
121
+
122
+ Examples:
123
+
124
+ ```typescript
125
+ const result = PrincipalSchema.safeParse(Principal.fromText("aaaaa-aa"));
126
+ console.log(result.success); // true or false
127
+ ```
128
+
129
+ [:link: Source](https://github.com/dfinity/icp-js-canisters/tree/main/packages/zod-schemas/src/principal.ts#L48)
91
130
 
92
131
  #### :gear: UrlSchema
93
132
 
@@ -102,6 +141,23 @@ Examples:
102
141
  UrlSchema.parse("https://example.com"); // Valid
103
142
  UrlSchema.parse("http://127.0.0.1"); // Valid (localhost exception)
104
143
 
105
- [:link: Source](https://github.com/dfinity/icp-js-canisters/tree/main/packages/zod-schemas/src/url.ts#L64)
144
+ [:link: Source](https://github.com/dfinity/icp-js-canisters/tree/main/packages/zod-schemas/src/url.ts#L65)
145
+
146
+ ### :nut_and_bolt: Enum
147
+
148
+ - [ZodSchemaId](#gear-zodschemaid)
149
+
150
+ #### :gear: ZodSchemaId
151
+
152
+ Enum of metadata `id` values assigned to Zod schemas in this library.
153
+
154
+ | Property | Type | Description |
155
+ | --------------- | ----------------- | --------------------------------------------- |
156
+ | `PrincipalText` | `"PrincipalText"` | Metadata id for {@link PrincipalTextSchema }. |
157
+ | `Principal` | `"Principal"` | Metadata id for {@link PrincipalSchema }. |
158
+ | `Uint8Array` | `"Uint8Array"` | Metadata id for {@link Uint8ArraySchema }. |
159
+ | `Url` | `"Url"` | Metadata id for {@link UrlSchema }. |
160
+
161
+ [:link: Source](https://github.com/dfinity/icp-js-canisters/tree/main/packages/zod-schemas/src/schema-id.ts#L4)
106
162
 
107
163
  <!-- TSDOC_END -->
@@ -0,0 +1,11 @@
1
+ import * as z from "zod";
2
+ /**
3
+ * Zod schema to validate a value is a `Uint8Array` instance.
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * const result = Uint8ArraySchema.safeParse(new Uint8Array([1, 2, 3]));
8
+ * console.log(result.success); // true or false
9
+ * ```
10
+ */
11
+ export declare const Uint8ArraySchema: z.ZodCustom<Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>>;
package/dist/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
+ export * from "./arrays";
1
2
  export * from "./principal";
3
+ export * from "./schema-id";
2
4
  export * from "./url";
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{Principal as i}from"@icp-sdk/core/principal";import*as n from"zod";var f=n.string().refine(r=>{try{return i.fromText(r),!0}catch{return!1}},{error:"Invalid textual representation of a Principal."});import*as l from"zod";var p=({additionalProtocols:r=[],allowHttpLocally:o=!0})=>l.url().refine(a=>{try{let t=[...new Set(["https:",...r])],{protocol:e,hostname:c}=new URL(a);return o&&["localhost","127.0.0.1"].includes(c)?["http:",...t].includes(e):t.includes(e)}catch{return!1}},{error:"Invalid URL."}),u=p({});export{f as PrincipalTextSchema,u as UrlSchema,p as createUrlSchema};
1
+ import*as l from"zod";var o=(t=>(t.PrincipalText="PrincipalText",t.Principal="Principal",t.Uint8Array="Uint8Array",t.Url="Url",t))(o||{});var P=l.instanceof(Uint8Array).meta({id:"Uint8Array"});import{Principal as n}from"@icp-sdk/core/principal";import*as e from"zod";var h=e.string().refine(r=>{try{return n.fromText(r),!0}catch{return!1}},{error:"Invalid textual representation of a Principal."}).meta({id:"PrincipalText"}),y=e.custom().refine(r=>n.isPrincipal(r),{error:"Invalid Principal.",abort:!0}).transform(r=>n.from(r)).meta({id:"Principal"});import*as c from"zod";var s=({additionalProtocols:r=[],allowHttpLocally:a=!0})=>c.url().refine(p=>{try{let i=[...new Set(["https:",...r])],{protocol:t,hostname:m}=new URL(p);return a&&["localhost","127.0.0.1"].includes(m)?["http:",...i].includes(t):i.includes(t)}catch{return!1}},{error:"Invalid URL."}),S=s({}).meta({id:"Url"});export{y as PrincipalSchema,h as PrincipalTextSchema,P as Uint8ArraySchema,S as UrlSchema,o as ZodSchemaId,s as createUrlSchema};
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../src/principal.ts", "../src/url.ts"],
4
- "sourcesContent": ["import { Principal } from \"@icp-sdk/core/principal\";\nimport * as z from \"zod\";\n\n/**\n * Zod schema to validate a string as a valid textual representation of a Principal.\n *\n * This schema checks if the provided string can be converted into a `Principal` instance.\n * If the conversion fails, validation will return an error message.\n *\n * @example\n * ```typescript\n * const result = PrincipalTextSchema.safeParse('aaaaa-aa');\n * console.log(result.success); // true or false\n * ```\n */\nexport const PrincipalTextSchema = z.string().refine(\n (principal) => {\n try {\n Principal.fromText(principal);\n return true;\n } catch (_err: unknown) {\n return false;\n }\n },\n {\n error: \"Invalid textual representation of a Principal.\",\n },\n);\n\nexport type PrincipalText = z.infer<typeof PrincipalTextSchema>;\n", "import * as z from \"zod\";\n\n/**\n * A URL protocol as template literal type.\n * Example: \"https:\" or \"ftp:\"\n */\nexport type UrlProtocol = `${string}:`;\n\n/**\n * Creates a Zod schema for validating URLs. By default, it validates that the URL protocol is HTTPS and allow usage of HTTP only locally.\n *\n * @param {Object} options - Configuration options for the schema.\n * @param {UrlProtocol[]} [options.additionalProtocols=[]] - Additional protocols to allow (e.g., \"wss:\" or \"ftp:\"). \u26A0\uFE0F Usage of insecure protocols is discouraged.\n * @param {boolean} [options.allowHttpLocally=true] - Whether to allow HTTP for localhost and 127.0.0.1. Default: true.\n * @returns {z.ZodEffects<z.ZodString, string, string>} - The Zod schema with URL validation.\n *\n * @example\n * const schema = createUrlSchema({\n * additionalProtocols: [\"wss:\"],\n * allowHttpLocally: false\n * });\n *\n * schema.parse(\"https://example.com\"); // Valid\n * schema.parse(\"wss://example.com\"); // Valid\n * schema.parse(\"http://localhost\"); // Invalid if allowHttpLocally is false\n */\nexport const createUrlSchema = ({\n additionalProtocols = [],\n allowHttpLocally = true,\n}: {\n additionalProtocols?: UrlProtocol[];\n allowHttpLocally?: boolean;\n}): z.ZodURL =>\n z.url().refine(\n (url: string | URL): boolean => {\n try {\n const protocols = [...new Set([\"https:\", ...additionalProtocols])];\n\n const { protocol, hostname } = new URL(url);\n\n // We allow http for development locally\n if (allowHttpLocally && [\"localhost\", \"127.0.0.1\"].includes(hostname)) {\n return [\"http:\", ...protocols].includes(protocol);\n }\n\n return protocols.includes(protocol);\n } catch (_err: unknown) {\n return false;\n }\n },\n {\n error: \"Invalid URL.\",\n },\n );\n\n/**\n * Default URL schema that enforces HTTPS and allows HTTP locally.\n *\n * @constant {z.ZodEffects<z.ZodString, string, string>}\n * @example\n * UrlSchema.parse(\"https://example.com\"); // Valid\n * UrlSchema.parse(\"http://127.0.0.1\"); // Valid (localhost exception)\n */\nexport const UrlSchema = createUrlSchema({});\n"],
5
- "mappings": "AAAA,OAAS,aAAAA,MAAiB,0BAC1B,UAAYC,MAAO,MAcZ,IAAMC,EAAwB,SAAO,EAAE,OAC3CC,GAAc,CACb,GAAI,CACF,OAAAH,EAAU,SAASG,CAAS,EACrB,EACT,MAAwB,CACtB,MAAO,EACT,CACF,EACA,CACE,MAAO,gDACT,CACF,EC3BA,UAAYC,MAAO,MA0BZ,IAAMC,EAAkB,CAAC,CAC9B,oBAAAC,EAAsB,CAAC,EACvB,iBAAAC,EAAmB,EACrB,IAII,MAAI,EAAE,OACLC,GAA+B,CAC9B,GAAI,CACF,IAAMC,EAAY,CAAC,GAAG,IAAI,IAAI,CAAC,SAAU,GAAGH,CAAmB,CAAC,CAAC,EAE3D,CAAE,SAAAI,EAAU,SAAAC,CAAS,EAAI,IAAI,IAAIH,CAAG,EAG1C,OAAID,GAAoB,CAAC,YAAa,WAAW,EAAE,SAASI,CAAQ,EAC3D,CAAC,QAAS,GAAGF,CAAS,EAAE,SAASC,CAAQ,EAG3CD,EAAU,SAASC,CAAQ,CACpC,MAAwB,CACtB,MAAO,EACT,CACF,EACA,CACE,MAAO,cACT,CACF,EAUWE,EAAYP,EAAgB,CAAC,CAAC",
6
- "names": ["Principal", "z", "PrincipalTextSchema", "principal", "z", "createUrlSchema", "additionalProtocols", "allowHttpLocally", "url", "protocols", "protocol", "hostname", "UrlSchema"]
3
+ "sources": ["../src/arrays.ts", "../src/schema-id.ts", "../src/principal.ts", "../src/url.ts"],
4
+ "sourcesContent": ["import * as z from \"zod\";\nimport { ZodSchemaId } from \"./schema-id\";\n\n/**\n * Zod schema to validate a value is a `Uint8Array` instance.\n *\n * @example\n * ```typescript\n * const result = Uint8ArraySchema.safeParse(new Uint8Array([1, 2, 3]));\n * console.log(result.success); // true or false\n * ```\n */\nexport const Uint8ArraySchema = z\n .instanceof(Uint8Array)\n .meta({ id: ZodSchemaId.Uint8Array });\n", "/**\n * Enum of metadata `id` values assigned to Zod schemas in this library.\n */\nexport enum ZodSchemaId {\n /** Metadata id for {@link PrincipalTextSchema}. */\n PrincipalText = \"PrincipalText\",\n /** Metadata id for {@link PrincipalSchema}. */\n Principal = \"Principal\",\n /** Metadata id for {@link Uint8ArraySchema}. */\n Uint8Array = \"Uint8Array\",\n /** Metadata id for {@link UrlSchema}. */\n Url = \"Url\",\n}\n", "import { Principal } from \"@icp-sdk/core/principal\";\nimport * as z from \"zod\";\nimport { ZodSchemaId } from \"./schema-id\";\n\n/**\n * Zod schema to validate a string as a valid textual representation of a Principal.\n *\n * This schema checks if the provided string can be converted into a `Principal` instance.\n * If the conversion fails, validation will return an error message.\n *\n * @example\n * ```typescript\n * const result = PrincipalTextSchema.safeParse('aaaaa-aa');\n * console.log(result.success); // true or false\n * ```\n */\nexport const PrincipalTextSchema = z\n .string()\n .refine(\n (principal) => {\n try {\n Principal.fromText(principal);\n return true;\n } catch (_err: unknown) {\n return false;\n }\n },\n {\n error: \"Invalid textual representation of a Principal.\",\n },\n )\n .meta({ id: ZodSchemaId.PrincipalText });\n\nexport type PrincipalText = z.infer<typeof PrincipalTextSchema>;\n\n/**\n * Zod schema to validate and transform a value into a `Principal` instance.\n *\n * This schema checks if the provided value is an instance or an object representing\n * a `Principal` and transforms it into a valid `Principal` instance.\n *\n * @example\n * ```typescript\n * const result = PrincipalSchema.safeParse(Principal.fromText('aaaaa-aa'));\n * console.log(result.success); // true or false\n * ```\n */\nexport const PrincipalSchema = z\n .custom<Principal>()\n .refine((principal) => Principal.isPrincipal(principal), {\n error: \"Invalid Principal.\",\n abort: true,\n })\n .transform((value) => Principal.from(value))\n .meta({ id: ZodSchemaId.Principal });\n", "import * as z from \"zod\";\nimport { ZodSchemaId } from \"./schema-id\";\n\n/**\n * A URL protocol as template literal type.\n * Example: \"https:\" or \"ftp:\"\n */\nexport type UrlProtocol = `${string}:`;\n\n/**\n * Creates a Zod schema for validating URLs. By default, it validates that the URL protocol is HTTPS and allow usage of HTTP only locally.\n *\n * @param {Object} options - Configuration options for the schema.\n * @param {UrlProtocol[]} [options.additionalProtocols=[]] - Additional protocols to allow (e.g., \"wss:\" or \"ftp:\"). \u26A0\uFE0F Usage of insecure protocols is discouraged.\n * @param {boolean} [options.allowHttpLocally=true] - Whether to allow HTTP for localhost and 127.0.0.1. Default: true.\n * @returns {z.ZodEffects<z.ZodString, string, string>} - The Zod schema with URL validation.\n *\n * @example\n * const schema = createUrlSchema({\n * additionalProtocols: [\"wss:\"],\n * allowHttpLocally: false\n * });\n *\n * schema.parse(\"https://example.com\"); // Valid\n * schema.parse(\"wss://example.com\"); // Valid\n * schema.parse(\"http://localhost\"); // Invalid if allowHttpLocally is false\n */\nexport const createUrlSchema = ({\n additionalProtocols = [],\n allowHttpLocally = true,\n}: {\n additionalProtocols?: UrlProtocol[];\n allowHttpLocally?: boolean;\n}): z.ZodURL =>\n z.url().refine(\n (url: string | URL): boolean => {\n try {\n const protocols = [...new Set([\"https:\", ...additionalProtocols])];\n\n const { protocol, hostname } = new URL(url);\n\n // We allow http for development locally\n if (allowHttpLocally && [\"localhost\", \"127.0.0.1\"].includes(hostname)) {\n return [\"http:\", ...protocols].includes(protocol);\n }\n\n return protocols.includes(protocol);\n } catch (_err: unknown) {\n return false;\n }\n },\n {\n error: \"Invalid URL.\",\n },\n );\n\n/**\n * Default URL schema that enforces HTTPS and allows HTTP locally.\n *\n * @constant {z.ZodEffects<z.ZodString, string, string>}\n * @example\n * UrlSchema.parse(\"https://example.com\"); // Valid\n * UrlSchema.parse(\"http://127.0.0.1\"); // Valid (localhost exception)\n */\nexport const UrlSchema = createUrlSchema({}).meta({ id: ZodSchemaId.Url });\n"],
5
+ "mappings": "AAAA,UAAYA,MAAO,MCGZ,IAAKC,OAEVA,EAAA,cAAgB,gBAEhBA,EAAA,UAAY,YAEZA,EAAA,WAAa,aAEbA,EAAA,IAAM,MARIA,OAAA,IDSL,IAAMC,EACV,aAAW,UAAU,EACrB,KAAK,CAAE,eAA2B,CAAC,EEdtC,OAAS,aAAAC,MAAiB,0BAC1B,UAAYC,MAAO,MAeZ,IAAMC,EACV,SAAO,EACP,OACEC,GAAc,CACb,GAAI,CACF,OAAAC,EAAU,SAASD,CAAS,EACrB,EACT,MAAwB,CACtB,MAAO,EACT,CACF,EACA,CACE,MAAO,gDACT,CACF,EACC,KAAK,CAAE,kBAA8B,CAAC,EAgB5BE,EACV,SAAkB,EAClB,OAAQF,GAAcC,EAAU,YAAYD,CAAS,EAAG,CACvD,MAAO,qBACP,MAAO,EACT,CAAC,EACA,UAAWG,GAAUF,EAAU,KAAKE,CAAK,CAAC,EAC1C,KAAK,CAAE,cAA0B,CAAC,ECtDrC,UAAYC,MAAO,MA2BZ,IAAMC,EAAkB,CAAC,CAC9B,oBAAAC,EAAsB,CAAC,EACvB,iBAAAC,EAAmB,EACrB,IAII,MAAI,EAAE,OACLC,GAA+B,CAC9B,GAAI,CACF,IAAMC,EAAY,CAAC,GAAG,IAAI,IAAI,CAAC,SAAU,GAAGH,CAAmB,CAAC,CAAC,EAE3D,CAAE,SAAAI,EAAU,SAAAC,CAAS,EAAI,IAAI,IAAIH,CAAG,EAG1C,OAAID,GAAoB,CAAC,YAAa,WAAW,EAAE,SAASI,CAAQ,EAC3D,CAAC,QAAS,GAAGF,CAAS,EAAE,SAASC,CAAQ,EAG3CD,EAAU,SAASC,CAAQ,CACpC,MAAwB,CACtB,MAAO,EACT,CACF,EACA,CACE,MAAO,cACT,CACF,EAUWE,EAAYP,EAAgB,CAAC,CAAC,EAAE,KAAK,CAAE,QAAoB,CAAC",
6
+ "names": ["z", "ZodSchemaId", "Uint8ArraySchema", "Principal", "z", "PrincipalTextSchema", "principal", "Principal", "PrincipalSchema", "value", "z", "createUrlSchema", "additionalProtocols", "allowHttpLocally", "url", "protocols", "protocol", "hostname", "UrlSchema"]
7
7
  }
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
1
  import { createRequire as topLevelCreateRequire } from 'module';
2
2
  const require = topLevelCreateRequire(import.meta.url);
3
- import{Principal as i}from"@icp-sdk/core/principal";import*as n from"zod";var f=n.string().refine(r=>{try{return i.fromText(r),!0}catch{return!1}},{error:"Invalid textual representation of a Principal."});import*as l from"zod";var p=({additionalProtocols:r=[],allowHttpLocally:o=!0})=>l.url().refine(a=>{try{let t=[...new Set(["https:",...r])],{protocol:e,hostname:c}=new URL(a);return o&&["localhost","127.0.0.1"].includes(c)?["http:",...t].includes(e):t.includes(e)}catch{return!1}},{error:"Invalid URL."}),u=p({});export{f as PrincipalTextSchema,u as UrlSchema,p as createUrlSchema};
3
+ import*as l from"zod";var o=(t=>(t.PrincipalText="PrincipalText",t.Principal="Principal",t.Uint8Array="Uint8Array",t.Url="Url",t))(o||{});var P=l.instanceof(Uint8Array).meta({id:"Uint8Array"});import{Principal as n}from"@icp-sdk/core/principal";import*as e from"zod";var h=e.string().refine(r=>{try{return n.fromText(r),!0}catch{return!1}},{error:"Invalid textual representation of a Principal."}).meta({id:"PrincipalText"}),y=e.custom().refine(r=>n.isPrincipal(r),{error:"Invalid Principal.",abort:!0}).transform(r=>n.from(r)).meta({id:"Principal"});import*as c from"zod";var s=({additionalProtocols:r=[],allowHttpLocally:a=!0})=>c.url().refine(p=>{try{let i=[...new Set(["https:",...r])],{protocol:t,hostname:m}=new URL(p);return a&&["localhost","127.0.0.1"].includes(m)?["http:",...i].includes(t):i.includes(t)}catch{return!1}},{error:"Invalid URL."}),S=s({}).meta({id:"Url"});export{y as PrincipalSchema,h as PrincipalTextSchema,P as Uint8ArraySchema,S as UrlSchema,o as ZodSchemaId,s as createUrlSchema};
4
4
  //# sourceMappingURL=index.mjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../src/principal.ts", "../src/url.ts"],
4
- "sourcesContent": ["import { Principal } from \"@icp-sdk/core/principal\";\nimport * as z from \"zod\";\n\n/**\n * Zod schema to validate a string as a valid textual representation of a Principal.\n *\n * This schema checks if the provided string can be converted into a `Principal` instance.\n * If the conversion fails, validation will return an error message.\n *\n * @example\n * ```typescript\n * const result = PrincipalTextSchema.safeParse('aaaaa-aa');\n * console.log(result.success); // true or false\n * ```\n */\nexport const PrincipalTextSchema = z.string().refine(\n (principal) => {\n try {\n Principal.fromText(principal);\n return true;\n } catch (_err: unknown) {\n return false;\n }\n },\n {\n error: \"Invalid textual representation of a Principal.\",\n },\n);\n\nexport type PrincipalText = z.infer<typeof PrincipalTextSchema>;\n", "import * as z from \"zod\";\n\n/**\n * A URL protocol as template literal type.\n * Example: \"https:\" or \"ftp:\"\n */\nexport type UrlProtocol = `${string}:`;\n\n/**\n * Creates a Zod schema for validating URLs. By default, it validates that the URL protocol is HTTPS and allow usage of HTTP only locally.\n *\n * @param {Object} options - Configuration options for the schema.\n * @param {UrlProtocol[]} [options.additionalProtocols=[]] - Additional protocols to allow (e.g., \"wss:\" or \"ftp:\"). \u26A0\uFE0F Usage of insecure protocols is discouraged.\n * @param {boolean} [options.allowHttpLocally=true] - Whether to allow HTTP for localhost and 127.0.0.1. Default: true.\n * @returns {z.ZodEffects<z.ZodString, string, string>} - The Zod schema with URL validation.\n *\n * @example\n * const schema = createUrlSchema({\n * additionalProtocols: [\"wss:\"],\n * allowHttpLocally: false\n * });\n *\n * schema.parse(\"https://example.com\"); // Valid\n * schema.parse(\"wss://example.com\"); // Valid\n * schema.parse(\"http://localhost\"); // Invalid if allowHttpLocally is false\n */\nexport const createUrlSchema = ({\n additionalProtocols = [],\n allowHttpLocally = true,\n}: {\n additionalProtocols?: UrlProtocol[];\n allowHttpLocally?: boolean;\n}): z.ZodURL =>\n z.url().refine(\n (url: string | URL): boolean => {\n try {\n const protocols = [...new Set([\"https:\", ...additionalProtocols])];\n\n const { protocol, hostname } = new URL(url);\n\n // We allow http for development locally\n if (allowHttpLocally && [\"localhost\", \"127.0.0.1\"].includes(hostname)) {\n return [\"http:\", ...protocols].includes(protocol);\n }\n\n return protocols.includes(protocol);\n } catch (_err: unknown) {\n return false;\n }\n },\n {\n error: \"Invalid URL.\",\n },\n );\n\n/**\n * Default URL schema that enforces HTTPS and allows HTTP locally.\n *\n * @constant {z.ZodEffects<z.ZodString, string, string>}\n * @example\n * UrlSchema.parse(\"https://example.com\"); // Valid\n * UrlSchema.parse(\"http://127.0.0.1\"); // Valid (localhost exception)\n */\nexport const UrlSchema = createUrlSchema({});\n"],
5
- "mappings": ";;AAAA,OAAS,aAAAA,MAAiB,0BAC1B,UAAYC,MAAO,MAcZ,IAAMC,EAAwB,SAAO,EAAE,OAC3CC,GAAc,CACb,GAAI,CACF,OAAAH,EAAU,SAASG,CAAS,EACrB,EACT,MAAwB,CACtB,MAAO,EACT,CACF,EACA,CACE,MAAO,gDACT,CACF,EC3BA,UAAYC,MAAO,MA0BZ,IAAMC,EAAkB,CAAC,CAC9B,oBAAAC,EAAsB,CAAC,EACvB,iBAAAC,EAAmB,EACrB,IAII,MAAI,EAAE,OACLC,GAA+B,CAC9B,GAAI,CACF,IAAMC,EAAY,CAAC,GAAG,IAAI,IAAI,CAAC,SAAU,GAAGH,CAAmB,CAAC,CAAC,EAE3D,CAAE,SAAAI,EAAU,SAAAC,CAAS,EAAI,IAAI,IAAIH,CAAG,EAG1C,OAAID,GAAoB,CAAC,YAAa,WAAW,EAAE,SAASI,CAAQ,EAC3D,CAAC,QAAS,GAAGF,CAAS,EAAE,SAASC,CAAQ,EAG3CD,EAAU,SAASC,CAAQ,CACpC,MAAwB,CACtB,MAAO,EACT,CACF,EACA,CACE,MAAO,cACT,CACF,EAUWE,EAAYP,EAAgB,CAAC,CAAC",
6
- "names": ["Principal", "z", "PrincipalTextSchema", "principal", "z", "createUrlSchema", "additionalProtocols", "allowHttpLocally", "url", "protocols", "protocol", "hostname", "UrlSchema"]
3
+ "sources": ["../src/arrays.ts", "../src/schema-id.ts", "../src/principal.ts", "../src/url.ts"],
4
+ "sourcesContent": ["import * as z from \"zod\";\nimport { ZodSchemaId } from \"./schema-id\";\n\n/**\n * Zod schema to validate a value is a `Uint8Array` instance.\n *\n * @example\n * ```typescript\n * const result = Uint8ArraySchema.safeParse(new Uint8Array([1, 2, 3]));\n * console.log(result.success); // true or false\n * ```\n */\nexport const Uint8ArraySchema = z\n .instanceof(Uint8Array)\n .meta({ id: ZodSchemaId.Uint8Array });\n", "/**\n * Enum of metadata `id` values assigned to Zod schemas in this library.\n */\nexport enum ZodSchemaId {\n /** Metadata id for {@link PrincipalTextSchema}. */\n PrincipalText = \"PrincipalText\",\n /** Metadata id for {@link PrincipalSchema}. */\n Principal = \"Principal\",\n /** Metadata id for {@link Uint8ArraySchema}. */\n Uint8Array = \"Uint8Array\",\n /** Metadata id for {@link UrlSchema}. */\n Url = \"Url\",\n}\n", "import { Principal } from \"@icp-sdk/core/principal\";\nimport * as z from \"zod\";\nimport { ZodSchemaId } from \"./schema-id\";\n\n/**\n * Zod schema to validate a string as a valid textual representation of a Principal.\n *\n * This schema checks if the provided string can be converted into a `Principal` instance.\n * If the conversion fails, validation will return an error message.\n *\n * @example\n * ```typescript\n * const result = PrincipalTextSchema.safeParse('aaaaa-aa');\n * console.log(result.success); // true or false\n * ```\n */\nexport const PrincipalTextSchema = z\n .string()\n .refine(\n (principal) => {\n try {\n Principal.fromText(principal);\n return true;\n } catch (_err: unknown) {\n return false;\n }\n },\n {\n error: \"Invalid textual representation of a Principal.\",\n },\n )\n .meta({ id: ZodSchemaId.PrincipalText });\n\nexport type PrincipalText = z.infer<typeof PrincipalTextSchema>;\n\n/**\n * Zod schema to validate and transform a value into a `Principal` instance.\n *\n * This schema checks if the provided value is an instance or an object representing\n * a `Principal` and transforms it into a valid `Principal` instance.\n *\n * @example\n * ```typescript\n * const result = PrincipalSchema.safeParse(Principal.fromText('aaaaa-aa'));\n * console.log(result.success); // true or false\n * ```\n */\nexport const PrincipalSchema = z\n .custom<Principal>()\n .refine((principal) => Principal.isPrincipal(principal), {\n error: \"Invalid Principal.\",\n abort: true,\n })\n .transform((value) => Principal.from(value))\n .meta({ id: ZodSchemaId.Principal });\n", "import * as z from \"zod\";\nimport { ZodSchemaId } from \"./schema-id\";\n\n/**\n * A URL protocol as template literal type.\n * Example: \"https:\" or \"ftp:\"\n */\nexport type UrlProtocol = `${string}:`;\n\n/**\n * Creates a Zod schema for validating URLs. By default, it validates that the URL protocol is HTTPS and allow usage of HTTP only locally.\n *\n * @param {Object} options - Configuration options for the schema.\n * @param {UrlProtocol[]} [options.additionalProtocols=[]] - Additional protocols to allow (e.g., \"wss:\" or \"ftp:\"). \u26A0\uFE0F Usage of insecure protocols is discouraged.\n * @param {boolean} [options.allowHttpLocally=true] - Whether to allow HTTP for localhost and 127.0.0.1. Default: true.\n * @returns {z.ZodEffects<z.ZodString, string, string>} - The Zod schema with URL validation.\n *\n * @example\n * const schema = createUrlSchema({\n * additionalProtocols: [\"wss:\"],\n * allowHttpLocally: false\n * });\n *\n * schema.parse(\"https://example.com\"); // Valid\n * schema.parse(\"wss://example.com\"); // Valid\n * schema.parse(\"http://localhost\"); // Invalid if allowHttpLocally is false\n */\nexport const createUrlSchema = ({\n additionalProtocols = [],\n allowHttpLocally = true,\n}: {\n additionalProtocols?: UrlProtocol[];\n allowHttpLocally?: boolean;\n}): z.ZodURL =>\n z.url().refine(\n (url: string | URL): boolean => {\n try {\n const protocols = [...new Set([\"https:\", ...additionalProtocols])];\n\n const { protocol, hostname } = new URL(url);\n\n // We allow http for development locally\n if (allowHttpLocally && [\"localhost\", \"127.0.0.1\"].includes(hostname)) {\n return [\"http:\", ...protocols].includes(protocol);\n }\n\n return protocols.includes(protocol);\n } catch (_err: unknown) {\n return false;\n }\n },\n {\n error: \"Invalid URL.\",\n },\n );\n\n/**\n * Default URL schema that enforces HTTPS and allows HTTP locally.\n *\n * @constant {z.ZodEffects<z.ZodString, string, string>}\n * @example\n * UrlSchema.parse(\"https://example.com\"); // Valid\n * UrlSchema.parse(\"http://127.0.0.1\"); // Valid (localhost exception)\n */\nexport const UrlSchema = createUrlSchema({}).meta({ id: ZodSchemaId.Url });\n"],
5
+ "mappings": ";;AAAA,UAAYA,MAAO,MCGZ,IAAKC,OAEVA,EAAA,cAAgB,gBAEhBA,EAAA,UAAY,YAEZA,EAAA,WAAa,aAEbA,EAAA,IAAM,MARIA,OAAA,IDSL,IAAMC,EACV,aAAW,UAAU,EACrB,KAAK,CAAE,eAA2B,CAAC,EEdtC,OAAS,aAAAC,MAAiB,0BAC1B,UAAYC,MAAO,MAeZ,IAAMC,EACV,SAAO,EACP,OACEC,GAAc,CACb,GAAI,CACF,OAAAC,EAAU,SAASD,CAAS,EACrB,EACT,MAAwB,CACtB,MAAO,EACT,CACF,EACA,CACE,MAAO,gDACT,CACF,EACC,KAAK,CAAE,kBAA8B,CAAC,EAgB5BE,EACV,SAAkB,EAClB,OAAQF,GAAcC,EAAU,YAAYD,CAAS,EAAG,CACvD,MAAO,qBACP,MAAO,EACT,CAAC,EACA,UAAWG,GAAUF,EAAU,KAAKE,CAAK,CAAC,EAC1C,KAAK,CAAE,cAA0B,CAAC,ECtDrC,UAAYC,MAAO,MA2BZ,IAAMC,EAAkB,CAAC,CAC9B,oBAAAC,EAAsB,CAAC,EACvB,iBAAAC,EAAmB,EACrB,IAII,MAAI,EAAE,OACLC,GAA+B,CAC9B,GAAI,CACF,IAAMC,EAAY,CAAC,GAAG,IAAI,IAAI,CAAC,SAAU,GAAGH,CAAmB,CAAC,CAAC,EAE3D,CAAE,SAAAI,EAAU,SAAAC,CAAS,EAAI,IAAI,IAAIH,CAAG,EAG1C,OAAID,GAAoB,CAAC,YAAa,WAAW,EAAE,SAASI,CAAQ,EAC3D,CAAC,QAAS,GAAGF,CAAS,EAAE,SAASC,CAAQ,EAG3CD,EAAU,SAASC,CAAQ,CACpC,MAAwB,CACtB,MAAO,EACT,CACF,EACA,CACE,MAAO,cACT,CACF,EAUWE,EAAYP,EAAgB,CAAC,CAAC,EAAE,KAAK,CAAE,QAAoB,CAAC",
6
+ "names": ["z", "ZodSchemaId", "Uint8ArraySchema", "Principal", "z", "PrincipalTextSchema", "principal", "Principal", "PrincipalSchema", "value", "z", "createUrlSchema", "additionalProtocols", "allowHttpLocally", "url", "protocols", "protocol", "hostname", "UrlSchema"]
7
7
  }
@@ -1,3 +1,4 @@
1
+ import { Principal } from "@icp-sdk/core/principal";
1
2
  import * as z from "zod";
2
3
  /**
3
4
  * Zod schema to validate a string as a valid textual representation of a Principal.
@@ -13,3 +14,16 @@ import * as z from "zod";
13
14
  */
14
15
  export declare const PrincipalTextSchema: z.ZodString;
15
16
  export type PrincipalText = z.infer<typeof PrincipalTextSchema>;
17
+ /**
18
+ * Zod schema to validate and transform a value into a `Principal` instance.
19
+ *
20
+ * This schema checks if the provided value is an instance or an object representing
21
+ * a `Principal` and transforms it into a valid `Principal` instance.
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * const result = PrincipalSchema.safeParse(Principal.fromText('aaaaa-aa'));
26
+ * console.log(result.success); // true or false
27
+ * ```
28
+ */
29
+ export declare const PrincipalSchema: z.ZodPipe<z.ZodCustom<Principal, Principal>, z.ZodTransform<Principal, Principal>>;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Enum of metadata `id` values assigned to Zod schemas in this library.
3
+ */
4
+ export declare enum ZodSchemaId {
5
+ /** Metadata id for {@link PrincipalTextSchema}. */
6
+ PrincipalText = "PrincipalText",
7
+ /** Metadata id for {@link PrincipalSchema}. */
8
+ Principal = "Principal",
9
+ /** Metadata id for {@link Uint8ArraySchema}. */
10
+ Uint8Array = "Uint8Array",
11
+ /** Metadata id for {@link UrlSchema}. */
12
+ Url = "Url"
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dfinity/zod-schemas",
3
- "version": "3.0.2",
3
+ "version": "3.1.0",
4
4
  "description": "A collection of reusable Zod schemas and validators for common data patterns in ICP applications",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",