UNPKG

@dfinity/zod-schemas

Version:

A collection of reusable Zod schemas and validators for common data patterns in ICP applications

8 lines (7 loc) 4.02 kB
{ "version": 3, "sources": ["../src/principal.ts", "../src/url.ts"], "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"], "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", "names": ["Principal", "z", "PrincipalTextSchema", "principal", "z", "createUrlSchema", "additionalProtocols", "allowHttpLocally", "url", "protocols", "protocol", "hostname", "UrlSchema"] }