What is benefit of ZOD in Nest JS?

By medeb | personalblog | 22 Oct 2023


Hello,

Let's suppose you are getting pieces of information from the backend and expect that there is a title and description and then in the real world the title becomes a name and then the description becomes information what happens exactly is Nest JS is compiling it from tsx to jsx and issue warning.

Then if you say type product is title that is string and description that is a string and then do a fetch request to backend like prisma or MongoDB or whichever ORM.

Then zod could solve all these problems.

So what you do exactly is to install zod library and create for example z.object({title:z.string().min(1),description:z.string().min(1)}) and assign to a constant like ProductSchema.

You could for example have a function like getting a description from a product and then it needs you infer that type by doing like const Product=z.infer(ProductSchema)

Then there is a validation of the unknown type received that if you do parse then an exception could be raised so most people could do safeparse and check if there is an error then return or there is data then do the rest of the programmation work.

ebf4ed17b663cfaffd9939d85f1050ad5f9edeac7eb116fd00f33cde66ed6c7b.png

How do you rate this article?

4



personalblog
personalblog

My daily experience in crypto world

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.