Use Zod Schemas partially using shape and unwrap

In this lesson we make use of partial validation of large and nested schemas in Zod.

Instead of creating many small schemas, which can complicate maintenance, Zod allows for the extraction of specific parts of a schema using the concept of ".shape". It "unwraps" the object schema, enabling access to sub-schemas within specific properties.

Additionally, we can use the .unwrap to get into element schemas of altered schemas such as .optional or .array.

Simplify working with complex schemas without the need to break them into hundreds of small schemas.

Share with a coworker

Transcript

[00:00] Sooner or later you will create big and nested schemas which will allow you to validate big and nested objects. However, at some point in time you will need to use only a part of the schema to run some partial validation. Now, you could extract the selected code into a separate variable with a separate schema. However, this would lead to creating lots of very small and atomic schemas and would make maintaining the overall structure quite difficult. So all in all, we might want to be able to use only a part of the schema from a bigger thing.

[00:38] So here we have a Zod object schema and on Zod object schemas we can use what is called a shape. So what is the difference between a shape and a schema? So a schema's primary purpose is to run validation against it. However, a shape somehow unwraps the object schema and allows to walk into certain properties. In other words if I have the schema and I would like to walk into a separate property this is impossible as we can run the parse or save parse etc so basically run the validation.

[01:18] However if you walk into the shape itself, then we can walk into separate sub-schemas that exist on separate properties. So this allows us to use some granular, some smaller parts of existing schemas. Now, let's walk into the guest details schema. In this case, what we're going to do is to walk into the room booking schema. We can walk into the shape.

[01:48] Now we walk into the guest details and in this case we have an array. So if you wanted to use the schema of a certain guest details element we want to walk into the element attribute over here and this way again we have a Zod object. Essentially we have access to what is being the element of the array. Now we could use the guest detail schema basically to run the parse. Now if we want to walk into the element of the amenities, so here we have an array of strings which is also optional in our case, what we might need to do is to walk into the room booking schema again, walk into shape, now amenities, so nothing new here.

[02:36] However, if we have something like optional, we might need to run what is unwrap. So here we are unwrapping the optional, since optional makes the value either be its original type or undefined. So we are unwrapping the thing and now we walk into the type of the element. So here we have just a Z.string. So this would be just the schema of an element.

[03:03] And we could again use it with the parse. Now, if we want to obtain the type of a nested schema, then to walk into the guest details element type, what we could do is to run the z.infer. Now we walk into the type of room booking schema, now shape, then guest details, and again to the element, And this is essentially the element of the type of the element of the guest details array. An alternative to obtain the very same structure would be to run room booking schema. And now, since we have the TypeScript Interface of the guest details of the room booking schema, we can just walk into the guest details schema the same way as we would walk in TypeScript.

[03:57] So this way, guest details is the interface with the three properties, and here the same, the one with the underscore just to have a unique name also has the same structure.