Using Zod to validate data in NestJS
Introduction This article will guide you on using Zod combined with Drizzle ORM to validate data such as payload body, url param, object and more. If you are using Drizzle ORM , Zod is almost the overwhelming choice thanks to its ability to automatically generate Schema from the Database , completely eliminating duplicate code typing and making the application type-safe. Besides using Zod , we also need to combine additional packages as follows drizzle-zod to automatically generate Zod schema from Database Tables . Helps automatically generate Zod Schemas directly from tables defined in Drizzle ORM. Avoids defining data twice (once in the database table in Drizzle, once creating Zod Schema to validate request body). Main features: Automatic mapping: Converts Drizzle data types (string, number, boolean, date, enum) into corresponding Zod schema. Flexible schema creation: Supports generating schema for inserted data ( createInsertSchema ), queried data ( createSelectSchema ) or upd...