[00:00] Using sets and maps in Zod is as simple as creating z.set with providing what is the schema of a set's value or in case of maps we need to create z.map and provide two schemas the key schema and the value schema So the difference relies on what sets and maps actually are in both JavaScript's runtime and TypeScript's compile time. So they correspond perfectly to what Zod schemas are in both JavaScript and TypeScript. So let's see this in action. We've got a guest detail schema and a room booking schema, the same as in previous lessons. And here we've got the room booking schema, which is inferred from the schema that we're going to use for the eventual validation.
[00:48] Now we have again an example booking object which has four standard properties that are not interesting for us and two properties, amenities, which we're going to turn into a set and a guest details, which we're going to turn into a map. So let's start by running ts-node against this file just to see that there are no errors and we're good to go. So let's start with creating the amenities. Amenities is going to be a Z.set. Now we need to define what is going to be the schema of the value.
[01:22] In our case, this is going to be simply a string. So let's run validation. Everything should be correct since we're not breaking the validation anywhere. So let's deliberately break the validation by providing a number. Let's run the validation and we will see that there is a code invalid type expected string received number And here the path says that on the top level of this object, under the amenities property, there is a value.
[01:54] Now this set is being dumped into an array and the index one of the array is being retrieved saying that this is the element that is troublesome anyhow. Now what is interesting is that we might want to customize this error message expected string received number. So let's just provide the message on the standard last property last So let's say that every amenity has to be a string. And let's see whether this error is being used here and we can see that no, the default message is being used and that's because we need to pay attention to where do we actually put this message on. Now, this message corresponds to the set itself and not the string itself.
[02:49] So we have put it on the wrong place. So if we put it over here on Z.string, now every amenity has to be a string. Now this is going to be used right now. Every amenity has to be a string. However, if we wanted to break something else, let's say that I'm going to remove this number over here, and we want to pass a array, which is just something that is not a set and we would like to see that amenities should be a set.
[03:26] Now this corresponds to the set level so if we pass something that is not a set, an array, then the correct message is going to be used as we can see, amenities should be a set. So if we take a look at the inferred type, we can see that amenities is just a pure TypeScript set of string values. So everything is aligned with TypeScript. So let's also create a map for the guest details. So this is going to be a Z dot map.
[04:00] Now, the key of the map in our case is simply z.string and the value is an object with the name, age and email. So in our case, this is the guest detail schema, which we're going to reuse over here. So let's save the file and rerun the validation to make sure that everything is up and running. So I just need to recreate the set so that everything is clear at this point. And yep, it is.
[04:32] So let's break something and I'm going to deliberately break age. And let's rerun the validation and we can see that there is an invalid type expected age to be number obviously and received undefined. So on guest details because again we started with a Zod object schema, under the guest details property, here internally again Zod is turning this map into an array of entries and on the entry with index 0, which is essentially this one over here, then under the value, so this value, not the key, but the value, there is a age property that should be defined on the schema, but it's not available over here. Again, if we take a look at the room booking schema, then we can also see, again, the same as we would do in TypeScript. This is a native TypeScript map with a string key and the value, which is an object which also gets automatically inferred.