pheralb/learning-zod(opens in a new tab)
Language: TypeScript
🔷 A REST api with Zod - TypeScript-first schema declaration and validation library.
- stars
- 5
- watchers
- 1
- forks
- 0
- open issues
- 0
- Last updated
- 10/28/2022
Language: TypeScript
🔷 A REST api with Zod - TypeScript-first schema declaration and validation library.
Template:
Snippets:
Tutorials:
Zod is a TypeScript-first schema declaration and validation library. They're using the term "schema" to broadly refer to any data type, from a simple string to a complex nested object.
This repository includes a REST api template created with:
And you will need to install Nodejs (+v18 LTS recommended) and a code editor, for example, Visual Studio Code.
git clone git@github.com:pheralb/learning-zod.git
cd learning-zod
npm installnpm run dev
By default, learning-zod is running on the port 3000. You can change the port here.
🔭 Commands:
npm run dev - Run the development server using ts-node-dev.npm run build - Build your rest api with SWC. It's very fast 😉.npm run build-tsc - If you have problems compiling with SWC, this command compiles the project with tsc.| - | Method | Url | - | Query Parameters |
|---|---|---|---|---|
| 📋 | POST | /login | Body - Json | {email: "hello@world.com", password:"mypassword123"} |
| 📋 | PUT | /products/1232?title=helloworld | Body - Json | {name: "iPhone", price:900} |
export const loginSchema = z.object({ body: z.object({ email: z.string().email({ message: "Write a correct email address", }), password: z.string().min(6, { message: "Password too short", }), }), });
URL: http://localhost:3000/login
{
"email": 11,
"password": []
}[
{
"field": ["email"],
"message": "Expected string, received number"
},
{
"field": ["password"],
"message": "Expected string, received array"
}
]URL: http://localhost:3000/login
{
"email": "abc",
"password": "123456gg"
}[
{
"field": ["email"],
"message": "Write a correct email address"
}
]const productSchema = z.object({ name: z.string().min(3, { message: "Name too short" }), price: z.number().min(0, { message: "Price must be positive" }), });
URL: http://localhost:3000/products
{}[
{
"field": ["name"],
"message": "Required"
},
{
"field": ["price"],
"message": "Required"
}
]URL: http://localhost:3000/products
{
"name": "iPhone",
"price": "33"
}[
{
"field": ["price"],
"message": "Expected number, received string"
}
]export const updateSchema = z.object({ name: z.string(), price: z.number().min(0, { message: "Price must be positive" }).optional(), });
URL: http://localhost:3000/products/1
{
"name": "iPhone",
"price": 30
}[
{
"field": ["params", "id"],
"message": "String must contain at least 3 character(s)"
}
]In the schema we request that a field is of type "string", for example, username:
const mySchema = z.object({ username: z.string(), });
If we introduce a number in username field, Zod returns the following:
{
"issues": [
{
"code": "invalid_type",
"expected": "string",
"received": "number",
"path": ["username"],
"message": "Expected string, received number"
}
],
"name": "ZodError"
}