Zod is a type-safety schema validator with super great DX and makes life easier in various way.
npm install zod # npm
yarn add zod # yarn
With Zod we can define schemas, like we used to do with Yup, and parse them to make sure that schema is met. Let's take a look at the simplest way to use it:
import { z } from 'zod'
const myStringSchema = z.string()
// let's try to parse some values
myStringSchema.parse('ilher') // this will return our 'ilher' since it's a valid value
myStringSchema.parse(0) // this will throw
We can even define and parse objects:
import { z } from 'zod'
const CoolObjectSchema = z.object({
hello: z.string(),
})
CoolObjectSchema.parse({ hello: 'world' })