Lesson
Validate maps and sets using Zod schemas
Use sets and maps in Zod for schema validation, ensuring type safety for both keys and values.
- Access
- Included
- Transcript
- Needs source
In this lesson we cover implementing sets and maps in Zod.
Using z.set, a schema for string-based amenities is created. We demonstrate how validation works by intentionally introducing errors and customizing error messages for better clarity.
Next, a map is created for existing guestDetails schema using z.map, with string keys and an existing z.object-based schema. The validation process is shown again by deliberately breaking the schema, such as omitting the age property, which triggers appropriate error messages.