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:
- Express: 4.18.2
- Typescript: 4.8.4
- ts-node-dev: 2.0.0
- tsconfig-paths: 4.1.0
- Zod: 3.19.1
- SWC: 0.1.57 (cli) & 1.3.11 (core).
And you will need to install Nodejs (+v18 LTS recommended) and a code editor, for example, Visual Studio Code.
- Clone the repository:
git clone [email protected]:pheralb/learning-zod.git
- Install dependencies:
cd learning-zod
npm install
- Run the development server:
npm 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: "[email protected]", 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",
}),
}),
});
π€ Test 1 - Method: POST.
URL: http:https://localhost:3000/login
- JSON body:
{
"email": 11,
"password": []
}
- Response:
[
{
"field": ["email"],
"message": "Expected string, received number"
},
{
"field": ["password"],
"message": "Expected string, received array"
}
]
π€ Test 2 - Method: POST.
URL: http:https://localhost:3000/login
- JSON body:
{
"email": "abc",
"password": "123456gg"
}
- Response:
[
{
"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" }),
});
π€ Test 1 - Method: POST.
URL: http:https://localhost:3000/products
- JSON body:
{}
- Response:
[
{
"field": ["name"],
"message": "Required"
},
{
"field": ["price"],
"message": "Required"
}
]
π€ Test 2 - Method: POST.
URL: http:https://localhost:3000/products
- JSON body:
{
"name": "iPhone",
"price": "33"
}
- Response:
[
{
"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(),
});
π€ Test 1 - Method: PUT.
URL: http:https://localhost:3000/products/1
- JSON body:
{
"name": "iPhone",
"price": 30
}
- Response:
[
{
"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"
}
- MIT.