• Stars
    star
    140
  • Rank 261,473 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

toZod

toZod is a utility for defining Zod schemas that agree with a TypeScript type.

This it the inverse how Zod typically works. By chaining and composing its built-in methods, Zod is able to build up an inferred static type for your schema. This is the opposite: toZod "infers" the structure of a Zod schema from a TS type.

Installation

yarn add tozod

âš  Requires TypeScript 3.9+ and "strictNullChecks": true âš 

Usage

import { toZod } from 'tozod';

type Player = {
  name: string;
  age?: number | undefined;
  active: boolean | null;
};

export const Player: toZod<Player> = z.object({
  name: z.string(),
  age: z.number().optional(),
  active: z.boolean().nullable(),
});

Getting rid of any of these method calls will throw a TypeError.

tozod type error screenshot

This gets extremely exciting when you start using it on recursive or mutually recursive types.

type User = {
  id: string;
  name: string;
  age?: number | undefined;
  active: boolean | null;
  posts: Post[];
};

type Post = {
  content: string;
  author: User;
};

export const User: toZod<User> = z.late.object(() => ({
  id: z.string().uuid(), // refinements are fine
  name: z.string(),
  age: z.number().optional(),
  active: z.boolean().nullable(),
  posts: z.array(Post),
}));

export const Post: toZod<Post> = z.late.object(() => ({
  content: z.string(),
  author: User,
}));

The above uses a z.late.object method that is currently implemented but undocumented.

You've just implemented two mutually recursive validatators with accurate static and runtime type information. So you can use Zod's built-in object methods to derive variants of these schemas:

const CreateUserInput = User.omit({ id: true, posts: true });
const PostIdOnly = Post.pick({ id: true });
const UpdateUserInput = User.omit({ id: true }).partial().extend({ id: z.string()u });

And because the TypeScript engine knows the exact shape of the Zod schema internally, you can access its internals like so:

User.shape.posts.element.shape.author;

More Repositories

1

zod

TypeScript-first schema validation with static type inference
TypeScript
33,028
star
2

devii

A developer blog starter for 2021 (Next.js + React + TypeScript + Markdown + syntax highlighting)
TypeScript
496
star
3

next-firebase-ssr

An Next.js example repo for building authenticated pages with Firebase Authentication, cookies, and getServerSideProps
TypeScript
419
star
4

nextjs-react-router

A demonstration of how to use React Router inside Next.js
TypeScript
132
star
5

bye-react

Command-line tool to migrate React to Preact
JavaScript
126
star
6

typejest

Test your types
TypeScript
26
star
7

normi

A zero-config normalized cache for MobX
TypeScript
20
star
8

zod-deno

TypeScript-first schema validation for Deno
TypeScript
16
star
9

trailhead

Find paths through your data in a type-safe way
TypeScript
12
star
10

nxtk

A TypeScript toolkit for Next.js
TypeScript
12
star
11

skee

Fluent schema modeling and migrations
TypeScript
8
star
12

npm-ts-starter

A project template for TypeScript npm packages
TypeScript
8
star
13

bun-workspaces

TypeScript
4
star
14

gwen

Utility-first CSS-in-JS styling library for maniacs
TypeScript
4
star
15

typesafe-api-no-graphql

A typesafe API — no GraphQL required
TypeScript
4
star
16

zod-locales

Localized error maps for Zod
3
star
17

DougNotes

Graphical dynamic knowledge repository platform
JavaScript
3
star
18

edgedb-movies

TypeScript
2
star
19

edgedb-nextjs-blog

TypeScript
2
star
20

live-typescript-monorepo

Strategies for live-updating TypeScript types in monorepos
TypeScript
1
star
21

edgedb-remix-test-2

JavaScript
1
star
22

kickdata

A scraper deployed to the Google App Engine that scrapes all soon-to-end Kickstarter projects every 10 minutes and stores relevant data in a Parse database.
Python
1
star
23

pensiv

Realtime Thoughtstreaming app with Meteor and Ionic
JavaScript
1
star
24

colinhacks

1
star
25

onepotpony

TypeScript
1
star
26

repros

TypeScript
1
star