Zod Section 10 exercises
solution

Refactoring Duplicated Schemas

Once again, there are a bunch ways that this code could be refactored.

For reference, here's what we started with:


const User = z.object({
id: z.string().uuid(),
name: z.string(),
});
const Post = z.object({
id: z.string().uuid(),
title: z.string(),
body: z.string(),
});

Loading solution

Transcript

0:00 There are a bunch of possible solutions here. The simplest solution is to strip out the ID thing here into its own type and then reference that within all the other z.objects, which is pretty good but we're still getting this object id: Id thing here. All of our cases are passing. This is OK.

0:19 Another solution would be to create a base object here, which is an ObjectWithId and then use the .extend function. What extend does is it creates a new type with the initial stuff here and the stuff that you add here.

0:37 What we end up with is we've got this ObjectWithId. That's the base of all of these pieces. If we add something else here, like created At which could be z.date, then this would also be shared across of the objects. That, obviously, wasn't the ID because we're going to get a lot of errors, then.

0:56 Another way you could do this is by using merge here. Merge, what it means, is you take the base ObjectWithId and you merge it with another object. You can see that this is slightly more verbose than the other version, then just extend, because here we're we get to pass in an object containing the name z.string.

1:17 Inside here, we actually have to pass a z.object. We have to wrap it in Zod. What this means is you can use this more generally to merge two different types together instead of just extending from them.

1:29 Those are different ways that you can compose objects together in Zod to reduce the amount of code duplication, make things more dry, and make things a bit more maintainable.