๐Ÿ“ฆ colinhacks / tozod

๐Ÿ“„ playground.ts ยท 46 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46import * as z from 'zod';
import { toZod } from '.';

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(),
});

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

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

const User: toZod<User> = z.late.object(() => ({
  name: z
    .string()
    .min(5)
    .max(2314)
    .refine(() => false, 'asdf'),
  age: z.number().optional(),
  active: z.boolean().nullable(),

  posts: z.array(Post),
}));

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

console.log(User.shape.posts.element.shape.author);