Skip to content

πŸ”· A REST api with Zod - TypeScript-first schema declaration and validation library.

License

Notifications You must be signed in to change notification settings

pheralb/learning-zod

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Learning Zod

GitHub stars GitHub issues GitHub license Required Node.JS +16

Template:

Snippets:

Tutorials:


πŸͺ Introduction

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.

πŸ‘¨β€πŸš€ Getting Started

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.

  1. Clone the repository:
git clone git@github.com:pheralb/learning-zod.git
  1. Install dependencies:
cd learning-zod
npm install
  1. 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.

πŸš€ Requests

- 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}

βœ”οΈ Validations

Login example schema:

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://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://localhost:3000/login

  • JSON body:
{
  "email": "abc",
  "password": "123456gg"
}
  • Response:
[
  {
    "field": ["email"],
    "message": "Write a correct email address"
  }
]

Product example schema:

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://localhost:3000/products

  • JSON body:
{}
  • Response:
[
  {
    "field": ["name"],
    "message": "Required"
  },
  {
    "field": ["price"],
    "message": "Required"
  }
]
πŸ€” Test 2 - Method: POST.

URL: http://localhost:3000/products

  • JSON body:
{
  "name": "iPhone",
  "price": "33"
}
  • Response:
[
  {
    "field": ["price"],
    "message": "Expected number, received string"
  }
]

Update product example schema:

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://localhost:3000/products/1

  • JSON body:
{
  "name": "iPhone",
  "price": 30
}
  • Response:
[
  {
    "field": ["params", "id"],
    "message": "String must contain at least 3 character(s)"
  }
]

πŸ“š Zod Error:

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"
}

πŸ”‘ License

About

πŸ”· A REST api with Zod - TypeScript-first schema declaration and validation library.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published