GitHunt
PH

pheralb/learning-zod

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

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

pheralb/learning-zod | GitHunt