npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

candid-to-zod

v1.0.2

Published

Convert Internet Computer Candid files (.did.js, .did.ts) into runtime-safe TypeScript Zod validation schemas.

Downloads

358

Readme

candid-to-zod ⚡️

npm version License: MIT

candid-to-zod is a powerful Node.js CLI tool and isomorphic library for the Internet Computer that instantly converts exported Candid types (.did.js, .did.ts) into runtime-safe TypeScript Zod Schemas.

Stop guessing what data structures your Canisters return! Guarantee end-to-end type safety across the IC boundary by ensuring your frontend strictly parses incoming canister data against robust Zod definitions.


🚀 Features

  • CLI Generation: Automatically compile Zod schemas natively in your terminal or CI/CD pipelines.
  • .ts & .js Support: Seamlessly analyzes dfx output files (e.g. export const init = ...).
  • Deep Types & Variants: Recursively maps nested Candid Vectors, Records, Options, and complex disjoint Variants into z.array, z.object, z.optional, and z.union.
  • Inferred Types: Automatically generates z.infer<> TypeScript types alongside your schemas.

🤔 Why do I need this? (Zod vs dfx Types)

If you're building on the Internet Computer, you're likely using the default TypeScript declarations generated by dfx generate. However, TypeScript is only a static compile-time tool. It does not exist at runtime!

If a canister is upgraded, if an API changes, or if an unexpected payload is returned from the blockchain, standard TypeScript will not block the corrupted data from entering your frontend app state—it will blindly assume the data matches the interface.

candid-to-zod solves this. By converting your Candid files into Zod schemas, you guarantee end-to-end runtime type safety. When you call zodSchema.parse(response), Zod actively strips unknown keys, verifies all required fields exist, and immediately throws a safe error if the blockchain data doesn't perfectly match your definitions!

📦 Installation

Install as a development dependency in your project:

npm install -D candid-to-zod

You'll also need the core Zod and Dfinity packages installed:

npm install zod @dfinity/principal @dfinity/candid

💻 Usage

1. Generate Schema via CLI

Add a script to your project's package.json to automatically build your schemas after running dfx generate:

{
  "scripts": {
    "generate:zod": "candid-to-zod -i ./src/declarations/my_canister/my_canister.did.js -o ./src/schemas/my_canister.ts"
  }
}

Then run:

npm run generate:zod

CLI Options

| Flag | Name | Description | default | |---|---|---|---| | -i, --input | Input Path | The generated .did.js or .did.ts Candid file | Required | | -o, --output | Output Path| Where to save the formatted .ts Zod schema | Required | | --no-infer | Skip Inferences | Skip generating export type X = z.infer<typeof XSchema>; | false |

[!TIP] Flexible Export Discovery: The CLI automatically detects your IDL factory even if it's not named idlFactory. It looks for any export ending in IdlFactory, making it compatible with all versions of dfx generate.

2. Validate Data in your App

Once your schemas have been generated, import them to rigorously type-check your Web3 requests!

import { my_canister } from "../declarations/my_canister";
import { getUserRetSchema } from "../schemas/my_canister.ts";

// 1. Fetch from your Canister
const rawResponse = await my_canister.getUser(principal);

// 2. Parse using Zod
// Strips unknown keys, guarantees runtime presence, and infers TypeScript return types natively!
const safeUser = getUserRetSchema.parse(rawResponse);

console.log(`Verified username: ${safeUser.username}`);

🏗 Using the Generator Programmatically (Library Mode)

You can also bypass the CLI and pass raw idlFactory objects directly to the generator function within the browser or Node:

import { generateZod } from "candid-to-zod";
import { idlFactory } from "./my_canister.did.js";

// Generates the Zod code as a string!
const stringifiedZodCode = generateZod(idlFactory, { inferTypes: true });

console.log(stringifiedZodCode);

🤝 Contributing

Built and maintained by Ibnyahyah. PRs and feature requests are welcome!

💖 Support the project: If you find this tool helpful, consider supporting the development at socio.kim/donate.