@dfinity/zod-schemas 1.0.0 → 2.0.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
@@ -47,6 +47,10 @@ Parameters:
47
47
  - `options.additionalProtocols`: - Additional protocols to allow (e.g., "wss:" or "ftp:"). ⚠️ Usage of insecure protocols is discouraged.
48
48
  - `options.allowHttpLocally`: - Whether to allow HTTP for localhost and 127.0.0.1. Default: true.
49
49
 
50
+ Returns:
51
+
52
+ - The Zod schema with URL validation.
53
+
50
54
  Examples:
51
55
 
52
56
  const schema = createUrlSchema({
@@ -1,2 +1,2 @@
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});
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")),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")),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/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",
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 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": "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,MAAwB,CACtB,MAAO,EACT,CACF,EACA,CACE,MAAO,gDACT,CACF,EC3BA,IAAAC,EAAmB,kBA0BNC,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
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*as r from"zod";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-NPK6AQOT.js.map
@@ -0,0 +1,7 @@
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.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,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": ["z", "createUrlSchema", "additionalProtocols", "allowHttpLocally", "url", "protocols", "protocol", "hostname", "UrlSchema"]
7
+ }
@@ -0,0 +1,2 @@
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}},{error:"Invalid textual representation of a Principal."});export{a};
2
+ //# sourceMappingURL=chunk-W6PRVPKK.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\";\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,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",
6
+ "names": ["Principal", "z", "PrincipalTextSchema", "principal"]
7
+ }
package/dist/esm/index.js CHANGED
@@ -1,2 +1,2 @@
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};
1
+ import{a as o}from"./chunk-W6PRVPKK.js";import{a as r,b as e}from"./chunk-NPK6AQOT.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-54D5E4U7.js";export{a as PrincipalTextSchema};
1
+ import{a}from"./chunk-W6PRVPKK.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-QUBJRYQG.js";export{b as UrlSchema,a as createUrlSchema};
1
+ import{a,b}from"./chunk-NPK6AQOT.js";export{b as UrlSchema,a as createUrlSchema};
2
2
  //# sourceMappingURL=url.js.map
@@ -1,4 +1,4 @@
1
- import * as z from "zod/v4";
1
+ import * as z from "zod";
2
2
  /**
3
3
  * Zod schema to validate a string as a valid textual representation of a Principal.
4
4
  *
@@ -1,4 +1,4 @@
1
- import * as z from "zod/v4";
1
+ import * as z from "zod";
2
2
  /**
3
3
  * A URL protocol as template literal type.
4
4
  * Example: "https:" or "ftp:"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dfinity/zod-schemas",
3
- "version": "1.0.0",
3
+ "version": "2.0.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
  "main": "dist/cjs/index.cjs.js",
@@ -16,7 +16,7 @@
16
16
  "ts-declaration": "tsc --emitDeclarationOnly --outDir dist/types",
17
17
  "build": "npm run rmdir && mkdir -p dist && node esbuild.mjs && npm run ts-declaration",
18
18
  "prepack": "npm run build",
19
- "test": "jest"
19
+ "test": "vitest"
20
20
  },
21
21
  "repository": {
22
22
  "type": "git",
@@ -48,6 +48,6 @@
48
48
  ],
49
49
  "peerDependencies": {
50
50
  "@dfinity/principal": "^2.0.0",
51
- "zod": "^3.25"
51
+ "zod": "^4"
52
52
  }
53
53
  }
@@ -1,2 +0,0 @@
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
@@ -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/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
- }
@@ -1,2 +0,0 @@
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
@@ -1,7 +0,0 @@
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
- }