A topic that can cause a lot of discussions:_ “What is the best way to parse and verify user input in Node.js?”_

Well, according to me it is Zod and today I am going to show you how to get started with Zod and why it is the absolute best!

🌱 Creating your first schema

First of all, we have to install Zod. You can do this with your favorite package manager.

npm install zod
// OR
yarn add zod

In our first schema, we are going to create one for a simple string.

Don’t worry we will get to more complicated validations later in this article.

All you need to do is:

  • Import Zod
  • Create a schema
  • Compare!

Let’s say we want a schema to parse a name. We want to put a few constraints on this name. We want it to: be a string, be at least 2 characters long and have a maximum length of 20 characters.

#typescript #coding #programming #javascript

Validating Input with Zod in TypeScript
3.40 GEEK