Define Unions and Intersections with Zod

In this lesson we'll use a few predefined schemas which will serve as constituents to create union and intersection schemas.

Three room booking schemas are defined: single, double, and suite. Each schema has slight differences in the structure.

The z.union utility is used to create a union of these schemas, allowing for validation against any of them. Type inference (using z.infer) for the room booking schema produces a union of object literals, exactly like in TypeScript.

We also create an intersection using z.intersection, combining room booking details with discount information.

These techniques enhance data validation and allow to make zod aligned even more with TypeScript.

Share with a coworker

Transcript

[00:00] Let's migrate the concept of unions and intersections from TypeScript to Zod. So in this example we have the single room booking schema, a double room booking schema and the suite room booking schema which are pretty similar to each other. The differences are the room type literal is slightly different, single, double, suite. The number of guests maximum value is slightly different, one, two, and four. And finally, the suite one has an amenities property which doesn't exist on the other ones.

[00:31] So they are quite similar to each other. Now we also have an example booking object which is of type suite and it conforms to the suite schema which is a number of guests which is a 3 and it has amenities but just to make it very explicit let's validate this file against ts-node and we would see that there is no error as for now. So the way to create a union in Zod is very straightforward and quite explicit. We're just going to run z.union. Now, what we need to pass here are the constituents of the union, which is in our case, single room booking schema, the double room booking schema, and the suite room booking schema.

[01:17] Let's also use this schema to update the code that runs the validation. So we're going to validate the booking object against the union. This is, of course, going to pass since if this one was passing, then the union that includes this constituent is also going to pass. Let's also see how this type gets inferred. So type room booking schema is going to be obviously z.infer of type of, and here comes the schema itself.

[01:52] And we would see that this is a union of this object literal so everything would work the same way as it would work in TypeScript. Let's also create an intersection. So this is the Z.intersection so very similar, very explicit. Here we don't pass the array, we just open the parentheses and we pass what is going to be the ingredients of the intersection so we're going to use the room booking schema itself. We're also going to use the discount schema which provides the discount code and discount amount.

[02:30] So let's use the discounted room booking schema that we have created over here and let's use it to run the validation. Let's run the file against ts-node and we would see that everything is up and running. Let's also create the type of this discounted room booking schema and z.infer of type of and here comes the schema itself and what we can see is that This is a union wrapped in parentheses, and same as in TypeScript, here we've got an intersection. So creating unions and intersections couldn't be easier in Zod.