JSPM

@dfinity/zod-schemas

2.1.0-next-2025-10-15.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3589
  • Score
    100M100P100Q137128F
  • License Apache-2.0

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

Package Exports

  • @dfinity/zod-schemas
  • @dfinity/zod-schemas/dist/cjs/index.cjs.js
  • @dfinity/zod-schemas/dist/esm/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@dfinity/zod-schemas) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Zod Schemas for apps on ICP

A collection of reusable Zod schemas and validators for common data patterns in Internet Computer applications.

npm version GitHub license

Table of contents

Installation

You can use the library by installing it in your project.

npm i @dfinity/zod-schemas

The bundle needs peer dependencies, be sure that following resources are available in your project as well.

npm i @dfinity/principal

Features

The library implements following features:

🧰 Functions

⚙️ 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; }) => ZodURL

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.

Returns:

  • The Zod schema with URL validation.

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

🔗 Source

🔧 Constants

⚙️ 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 ZodString

Examples:

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

🔗 Source

⚙️ UrlSchema

Default URL schema that enforces HTTPS and allows HTTP locally.

Constant Type
UrlSchema ZodURL

Examples:

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

🔗 Source