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 🙏

© 2024 – Pkg Stats / Ryan Hefner

zodify

v1.1.5

Published

A CLI util to generate Zod schemas from TypeScript code

Downloads

8

Readme

This CLI util can help you generate Zod schemas from interface / type and enum declarations.

This util always assumes that your TypeScript code is valid, and tsconfig.json has been set properly. Garbage in, Garbage out.

Node.js Requirement: >= 16

Usage

cd my-project
npm install zodify --save-dev
zodify --out-dir ./src/api/schemas

Your TypeScript code (assume that the path is src/a-random-directory/model.ts) may look like:

import type { Genders } from './gender'
type Name = string;

/*
 * @schema
 */
export interface Person {
  name: Name;
  age: number;
  gender: Genders;
}

use jsdoc-style comment to mark the types you want to transform to zod schemas (in this example, use a @schema tag as the mark).

Generated zod schema file (in ./src/api/schemas/model.ts) will look like this:

import { z } from "zod";
import { gendersSchema } from "./gender"

const nameSchema = z.string();
export const personSchema = z.object({
  name: nameSchema,
  age: z.number(),
  gender: gendersSchema
});

All options:

  • --tsconfig <tsconfig>: Optional. tsconfig path of this project. Zodify will use tsconfig to find proper files(respecting files / include/exclude fields etc.) in the code base. Zodify will find proper tsconfig file if omitted
  • --out-dir <path>: Required. the directory for generated zod schema files. ** Caution: this directory will be emptied every time zodify works**
  • --pattern <pattern>: Optional. A regex string for filter files which were found by zodify. For example: --pattern ^src/models let zodify find schema types under src/models.
  • --tag <tag>: Optional. The tag string used in comment. Default: 'schema'
  • --name-style <nameStyle>: Optional. The naming style you want to use for zod schemas

use zodify --help to see details.

TypeScript & Zod features & Limitations

  • Features that can be transformed between TypeScript and Zod are supported.
  • A z.unknown() will be generated if Zodify find it can not be transformed from TypeScript code to Zod schema. You will see warning when this happens.
  • Types defined in global modules will not be recognized and transformed.

Caution: Always use TypeScript to check schema

You should always make generated Zod schemas checked by TypeScript, to ensure their correctness.

For example, if you want to use generated zod schemas to validate HTTP response, your http request function will look like this:

import type { ZodSchema } from "zod";
import type { Person } from './types'; // this is your source interface
import { personSchema } from './your-schemas-dir'; // this is schema generated by Zodify

function httpGet<T>(url:string, schema: ZodSchema<T>) {}


// call function with generics type parameter
// if personSchema is not correct, TypeScript will reveal it.
httpGet<Person>('/person', personSchema);

Bug Report & Contribute

If you find any bug, please raise an issue.

Roadmap

  • [ ] Unit Tests
  • [ ] Support Recursive Types
  • [ ] Support type refinement by comments
  • [ ] Support Valibot