@dfinity/zod-schemas 0.0.2 → 1.0.0-beta-2025-06-26

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
@@ -37,9 +37,9 @@ The library implements following features:
37
37
 
38
38
  Creates a Zod schema for validating URLs. By default, it validates that the URL protocol is HTTPS and allow usage of HTTP only locally.
39
39
 
40
- | Function | Type |
41
- | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
42
- | `createUrlSchema` | `({ additionalProtocols, allowHttpLocally, }: { additionalProtocols?: `${string}:`[] or undefined; allowHttpLocally?: boolean or undefined; }) => ZodEffects<ZodString, string, string>` |
40
+ | Function | Type |
41
+ | ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
42
+ | `createUrlSchema` | `({ additionalProtocols, allowHttpLocally, }: { additionalProtocols?: `${string}:`[] or undefined; allowHttpLocally?: boolean or undefined; }) => ZodURL` |
43
43
 
44
44
  Parameters:
45
45
 
@@ -72,9 +72,9 @@ Zod schema to validate a string as a valid textual representation of a Principal
72
72
  This schema checks if the provided string can be converted into a `Principal` instance.
73
73
  If the conversion fails, validation will return an error message.
74
74
 
75
- | Constant | Type |
76
- | --------------------- | --------------------------------------- |
77
- | `PrincipalTextSchema` | `ZodEffects<ZodString, string, string>` |
75
+ | Constant | Type |
76
+ | --------------------- | ----------- |
77
+ | `PrincipalTextSchema` | `ZodString` |
78
78
 
79
79
  Examples:
80
80
 
@@ -89,15 +89,15 @@ console.log(result.success); // true or false
89
89
 
90
90
  Default URL schema that enforces HTTPS and allows HTTP locally.
91
91
 
92
- | Constant | Type |
93
- | ----------- | --------------------------------------- |
94
- | `UrlSchema` | `ZodEffects<ZodString, string, string>` |
92
+ | Constant | Type |
93
+ | ----------- | -------- |
94
+ | `UrlSchema` | `ZodURL` |
95
95
 
96
96
  Examples:
97
97
 
98
98
  UrlSchema.parse("https://example.com"); // Valid
99
99
  UrlSchema.parse("http://127.0.0.1"); // Valid (localhost exception)
100
100
 
101
- [:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/zod-schemas/src/url.ts#L70)
101
+ [:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/zod-schemas/src/url.ts#L64)
102
102
 
103
103
  <!-- TSDOC_END -->
@@ -1,2 +1,2 @@
1
- "use strict";var u=Object.create;var i=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var d=Object.getPrototypeOf,g=Object.prototype.hasOwnProperty;var P=(r,t)=>{for(var e in t)i(r,e,{get:t[e],enumerable:!0})},l=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of h(t))!g.call(r,o)&&o!==e&&i(r,o,{get:()=>t[o],enumerable:!(n=x(t,o))||n.enumerable});return r};var a=(r,t,e)=>(e=r!=null?u(d(r)):{},l(t||!r||!r.__esModule?i(e,"default",{value:r,enumerable:!0}):e,r)),z=r=>l(i({},"__esModule",{value:!0}),r);var S={};P(S,{PrincipalTextSchema:()=>U,UrlSchema:()=>y,createUrlSchema:()=>f});module.exports=z(S);var c=require("@dfinity/principal"),s=a(require("zod")),U=s.string().refine(r=>{try{return c.Principal.fromText(r),!0}catch{return!1}},{message:"Invalid textual representation of a Principal."});var p=a(require("zod")),f=({additionalProtocols:r=[],allowHttpLocally:t=!0})=>p.string().url().refine(e=>{try{let n=[...new Set(["https:",...r])],{protocol:o,hostname:m}=new URL(e);return t&&["localhost","127.0.0.1"].includes(m)?["http:",...n].includes(o):n.includes(o)}catch{return!1}},{message:"Invalid URL."}),y=f({});0&&(module.exports={PrincipalTextSchema,UrlSchema,createUrlSchema});
1
+ "use strict";var u=Object.create;var l=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var d=Object.getPrototypeOf,P=Object.prototype.hasOwnProperty;var U=(r,t)=>{for(var o in t)l(r,o,{get:t[o],enumerable:!0})},a=(r,t,o,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of h(t))!P.call(r,e)&&e!==o&&l(r,e,{get:()=>t[e],enumerable:!(n=x(t,e))||n.enumerable});return r};var c=(r,t,o)=>(o=r!=null?u(d(r)):{},a(t||!r||!r.__esModule?l(o,"default",{value:r,enumerable:!0}):o,r)),y=r=>a(l({},"__esModule",{value:!0}),r);var L={};U(L,{PrincipalTextSchema:()=>z,UrlSchema:()=>w,createUrlSchema:()=>f});module.exports=y(L);var i=require("@dfinity/principal"),p=c(require("zod/v4")),z=p.string().refine(r=>{try{return i.Principal.fromText(r),!0}catch{return!1}},{error:"Invalid textual representation of a Principal."});var s=c(require("zod/v4")),f=({additionalProtocols:r=[],allowHttpLocally:t=!0})=>s.url().refine(o=>{try{let n=[...new Set(["https:",...r])],{protocol:e,hostname:m}=new URL(o);return t&&["localhost","127.0.0.1"].includes(m)?["http:",...n].includes(e):n.includes(e)}catch{return!1}},{error:"Invalid URL."}),w=f({});0&&(module.exports={PrincipalTextSchema,UrlSchema,createUrlSchema});
2
2
  //# sourceMappingURL=index.cjs.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts", "../../src/principal.ts", "../../src/url.ts"],
4
- "sourcesContent": ["export * from \"./principal\";\nexport * from \"./url\";\n", "import { Principal } from \"@dfinity/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 message: \"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.ZodEffects<z.ZodString, string, string> =>\n z\n .string()\n .url()\n .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 (\n allowHttpLocally &&\n [\"localhost\", \"127.0.0.1\"].includes(hostname)\n ) {\n return [\"http:\", ...protocols].includes(protocol);\n }\n\n return protocols.includes(protocol);\n } catch (_err: unknown) {\n return false;\n }\n },\n {\n message: \"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": "0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,yBAAAE,EAAA,cAAAC,EAAA,oBAAAC,IAAA,eAAAC,EAAAL,GCAA,IAAAM,EAA0B,8BAC1BC,EAAmB,kBAcNC,EAAwB,SAAO,EAAE,OAC3CC,GAAc,CACb,GAAI,CACF,mBAAU,SAASA,CAAS,EACrB,EACT,MAAuB,CACrB,MAAO,EACT,CACF,EACA,CACE,QAAS,gDACX,CACF,EC3BA,IAAAC,EAAmB,kBA0BNC,EAAkB,CAAC,CAC9B,oBAAAC,EAAsB,CAAC,EACvB,iBAAAC,EAAmB,EACrB,IAKK,SAAO,EACP,IAAI,EACJ,OACEC,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,OACED,GACA,CAAC,YAAa,WAAW,EAAE,SAASI,CAAQ,EAErC,CAAC,QAAS,GAAGF,CAAS,EAAE,SAASC,CAAQ,EAG3CD,EAAU,SAASC,CAAQ,CACpC,MAAwB,CACtB,MAAO,EACT,CACF,EACA,CACE,QAAS,cACX,CACF,EAUSE,EAAYP,EAAgB,CAAC,CAAC",
6
- "names": ["src_exports", "__export", "PrincipalTextSchema", "UrlSchema", "createUrlSchema", "__toCommonJS", "import_principal", "z", "PrincipalTextSchema", "principal", "z", "createUrlSchema", "additionalProtocols", "allowHttpLocally", "url", "protocols", "protocol", "hostname", "UrlSchema"]
4
+ "sourcesContent": ["export * from \"./principal\";\nexport * from \"./url\";\n", "import { Principal } from \"@dfinity/principal\";\nimport * as z from \"zod/v4\";\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/v4\";\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": "0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,yBAAAE,EAAA,cAAAC,EAAA,oBAAAC,IAAA,eAAAC,EAAAL,GCAA,IAAAM,EAA0B,8BAC1BC,EAAmB,qBAcNC,EAAwB,SAAO,EAAE,OAC3CC,GAAc,CACb,GAAI,CACF,mBAAU,SAASA,CAAS,EACrB,EACT,MAAwB,CACtB,MAAO,EACT,CACF,EACA,CACE,MAAO,gDACT,CACF,EC3BA,IAAAC,EAAmB,qBA0BNC,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": ["index_exports", "__export", "PrincipalTextSchema", "UrlSchema", "createUrlSchema", "__toCommonJS", "import_principal", "z", "PrincipalTextSchema", "principal", "z", "createUrlSchema", "additionalProtocols", "allowHttpLocally", "url", "protocols", "protocol", "hostname", "UrlSchema"]
7
7
  }
@@ -0,0 +1,2 @@
1
+ import{Principal as t}from"@dfinity/principal";import*as r from"zod/v4";var a=r.string().refine(e=>{try{return t.fromText(e),!0}catch{return!1}},{error:"Invalid textual representation of a Principal."});export{a};
2
+ //# sourceMappingURL=chunk-54D5E4U7.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/principal.ts"],
4
+ "sourcesContent": ["import { Principal } from \"@dfinity/principal\";\nimport * as z from \"zod/v4\";\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"],
5
+ "mappings": "AAAA,OAAS,aAAAA,MAAiB,qBAC1B,UAAYC,MAAO,SAcZ,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",
6
+ "names": ["Principal", "z", "PrincipalTextSchema", "principal"]
7
+ }
@@ -0,0 +1,2 @@
1
+ import*as r from"zod/v4";var s=({additionalProtocols:e=[],allowHttpLocally:l=!0})=>r.url().refine(n=>{try{let o=[...new Set(["https:",...e])],{protocol:t,hostname:c}=new URL(n);return l&&["localhost","127.0.0.1"].includes(c)?["http:",...o].includes(t):o.includes(t)}catch{return!1}},{error:"Invalid URL."}),a=s({});export{s as a,a as b};
2
+ //# sourceMappingURL=chunk-QUBJRYQG.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/url.ts"],
4
+ "sourcesContent": ["import * as z from \"zod/v4\";\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,UAAYA,MAAO,SA0BZ,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": ["z", "createUrlSchema", "additionalProtocols", "allowHttpLocally", "url", "protocols", "protocol", "hostname", "UrlSchema"]
7
+ }
package/dist/esm/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{a as o}from"./chunk-23MH567F.js";import{a as r,b as e}from"./chunk-PXRHZW6E.js";export{o as PrincipalTextSchema,e as UrlSchema,r as createUrlSchema};
1
+ import{a as o}from"./chunk-54D5E4U7.js";import{a as r,b as e}from"./chunk-QUBJRYQG.js";export{o as PrincipalTextSchema,e as UrlSchema,r as createUrlSchema};
2
2
  //# sourceMappingURL=index.js.map
@@ -1,2 +1,2 @@
1
- import{a}from"./chunk-23MH567F.js";export{a as PrincipalTextSchema};
1
+ import{a}from"./chunk-54D5E4U7.js";export{a as PrincipalTextSchema};
2
2
  //# sourceMappingURL=principal.js.map
package/dist/esm/url.js CHANGED
@@ -1,2 +1,2 @@
1
- import{a,b}from"./chunk-PXRHZW6E.js";export{b as UrlSchema,a as createUrlSchema};
1
+ import{a,b}from"./chunk-QUBJRYQG.js";export{b as UrlSchema,a as createUrlSchema};
2
2
  //# sourceMappingURL=url.js.map
@@ -1,4 +1,4 @@
1
- import * as z from "zod";
1
+ import * as z from "zod/v4";
2
2
  /**
3
3
  * Zod schema to validate a string as a valid textual representation of a Principal.
4
4
  *
@@ -11,5 +11,5 @@ import * as z from "zod";
11
11
  * console.log(result.success); // true or false
12
12
  * ```
13
13
  */
14
- export declare const PrincipalTextSchema: z.ZodEffects<z.ZodString, string, string>;
14
+ export declare const PrincipalTextSchema: z.ZodString;
15
15
  export type PrincipalText = z.infer<typeof PrincipalTextSchema>;
@@ -1,4 +1,4 @@
1
- import * as z from "zod";
1
+ import * as z from "zod/v4";
2
2
  /**
3
3
  * A URL protocol as template literal type.
4
4
  * Example: "https:" or "ftp:"
@@ -25,7 +25,7 @@ export type UrlProtocol = `${string}:`;
25
25
  export declare const createUrlSchema: ({ additionalProtocols, allowHttpLocally, }: {
26
26
  additionalProtocols?: UrlProtocol[];
27
27
  allowHttpLocally?: boolean;
28
- }) => z.ZodEffects<z.ZodString, string, string>;
28
+ }) => z.ZodURL;
29
29
  /**
30
30
  * Default URL schema that enforces HTTPS and allows HTTP locally.
31
31
  *
@@ -34,4 +34,4 @@ export declare const createUrlSchema: ({ additionalProtocols, allowHttpLocally,
34
34
  * UrlSchema.parse("https://example.com"); // Valid
35
35
  * UrlSchema.parse("http://127.0.0.1"); // Valid (localhost exception)
36
36
  */
37
- export declare const UrlSchema: z.ZodEffects<z.ZodString, string, string>;
37
+ export declare const UrlSchema: z.ZodURL;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dfinity/zod-schemas",
3
- "version": "0.0.2",
3
+ "version": "1.0.0-beta-2025-06-26",
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
  "main": "dist/cjs/index.cjs.js",
@@ -47,7 +47,7 @@
47
47
  "service-nervous-system"
48
48
  ],
49
49
  "peerDependencies": {
50
- "@dfinity/principal": "^2.0.0",
51
- "zod": "^3"
50
+ "@dfinity/principal": "*",
51
+ "zod": "*"
52
52
  }
53
- }
53
+ }
@@ -1,2 +0,0 @@
1
- import{Principal as t}from"@dfinity/principal";import*as r from"zod";var a=r.string().refine(e=>{try{return t.fromText(e),!0}catch{return!1}},{message:"Invalid textual representation of a Principal."});export{a};
2
- //# sourceMappingURL=chunk-23MH567F.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/principal.ts"],
4
- "sourcesContent": ["import { Principal } from \"@dfinity/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 message: \"Invalid textual representation of a Principal.\",\n },\n);\n\nexport type PrincipalText = z.infer<typeof PrincipalTextSchema>;\n"],
5
- "mappings": "AAAA,OAAS,aAAAA,MAAiB,qBAC1B,UAAYC,MAAO,MAcZ,IAAMC,EAAwB,SAAO,EAAE,OAC3CC,GAAc,CACb,GAAI,CACF,OAAAH,EAAU,SAASG,CAAS,EACrB,EACT,MAAuB,CACrB,MAAO,EACT,CACF,EACA,CACE,QAAS,gDACX,CACF",
6
- "names": ["Principal", "z", "PrincipalTextSchema", "principal"]
7
- }
@@ -1,2 +0,0 @@
1
- import*as r from"zod";var c=({additionalProtocols:e=[],allowHttpLocally:n=!0})=>r.string().url().refine(l=>{try{let t=[...new Set(["https:",...e])],{protocol:o,hostname:s}=new URL(l);return n&&["localhost","127.0.0.1"].includes(s)?["http:",...t].includes(o):t.includes(o)}catch{return!1}},{message:"Invalid URL."}),a=c({});export{c as a,a as b};
2
- //# sourceMappingURL=chunk-PXRHZW6E.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/url.ts"],
4
- "sourcesContent": ["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.ZodEffects<z.ZodString, string, string> =>\n z\n .string()\n .url()\n .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 (\n allowHttpLocally &&\n [\"localhost\", \"127.0.0.1\"].includes(hostname)\n ) {\n return [\"http:\", ...protocols].includes(protocol);\n }\n\n return protocols.includes(protocol);\n } catch (_err: unknown) {\n return false;\n }\n },\n {\n message: \"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,UAAYA,MAAO,MA0BZ,IAAMC,EAAkB,CAAC,CAC9B,oBAAAC,EAAsB,CAAC,EACvB,iBAAAC,EAAmB,EACrB,IAKK,SAAO,EACP,IAAI,EACJ,OACEC,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,OACED,GACA,CAAC,YAAa,WAAW,EAAE,SAASI,CAAQ,EAErC,CAAC,QAAS,GAAGF,CAAS,EAAE,SAASC,CAAQ,EAG3CD,EAAU,SAASC,CAAQ,CACpC,MAAwB,CACtB,MAAO,EACT,CACF,EACA,CACE,QAAS,cACX,CACF,EAUSE,EAAYP,EAAgB,CAAC,CAAC",
6
- "names": ["z", "createUrlSchema", "additionalProtocols", "allowHttpLocally", "url", "protocols", "protocol", "hostname", "UrlSchema"]
7
- }