Zod Section 10 exercises

Reduce Duplicated Code by Composing Schemas

And now for something completely different.

In this exercise, we'll be looking at techniques how to refactor working code to remove duplication.

Here we have schemas for User, Post, and Comment:

const User = z.object({
id: z.string().uuid(),
name: z.string(),
const Po

Loading exercise


0:00 This exercise is a little bit different from the others because it's a refactor exercise. We've got some user and post and comment thing down here. What this is doing is there's a lot of duplicated code here. This ID is duplicated a bunch of times. There are various things in Zod that help you compose objects together into different types.

0:22 I've got some cases down here. Don't worry about the complicated TypeScript syntax here. What it's basically doing is we've got z.infer here inside this equal. If I just break this out a little bit, what equal is doing is it's making sure that this and this are of the same type.

0:42 If I say, "Comment," if I just remove the ID there...Sorry. That was in user there. User, it's not equal, these two things. Type false does not satisfy the constraint true because it doesn't have the ID property anymore.

0:59 Consider these three things of comment, post, and user. These are like your guides to make sure that the objects still represent what I want them to represent. Of course, if you run the exercise, then it's going to run the type check. If they're not passing, then it's going to tell you. Your challenge is to try to use some Zod APIs to make these objects a bit cleaner and a bit more dry.