{"id":13767350,"url":"https://github.com/roziscoding/zod-express-guard","last_synced_at":"2025-03-21T07:31:06.891Z","repository":{"id":57406394,"uuid":"319152028","full_name":"roziscoding/zod-express-guard","owner":"roziscoding","description":"Small package intended to use zod to make express request type-safe.","archived":false,"fork":false,"pushed_at":"2023-10-09T13:40:14.000Z","size":1138,"stargazers_count":24,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-17T11:55:26.671Z","etag":null,"topics":["express","hacktoberfest","nodejs","typescript","validation"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/roziscoding.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-12-06T23:16:27.000Z","updated_at":"2024-08-23T18:18:33.000Z","dependencies_parsed_at":"2024-01-07T03:53:15.595Z","dependency_job_id":null,"html_url":"https://github.com/roziscoding/zod-express-guard","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roziscoding%2Fzod-express-guard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roziscoding%2Fzod-express-guard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roziscoding%2Fzod-express-guard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roziscoding%2Fzod-express-guard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/roziscoding","download_url":"https://codeload.github.com/roziscoding/zod-express-guard/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244119810,"owners_count":20401053,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express","hacktoberfest","nodejs","typescript","validation"],"created_at":"2024-08-03T16:01:07.785Z","updated_at":"2025-03-21T07:31:06.600Z","avatar_url":"https://github.com/roziscoding.png","language":"TypeScript","readme":"# zod-express-guard\n\nSmall package intended to use zod to make express request type-safe.\n\n---\n\n\u003c!-- all-shields/badges:START --\u003e\n\n[![](https://img.shields.io/github/actions/workflow/status/roziscoding/zod-express-guard/nodejs.yml?label=tests\u0026style=flat-square\u0026branch=main)](https://github.com/roziscoding/zod-express-guard/actions?query=workflow%3A%22Node.js+CI%22) [![](https://img.shields.io/badge/latest-v1.0.5-CB3837.svg?style=flat-square\u0026logo=npm)](https://www.npmjs.com/package/zod-express-guard) [![](https://img.shields.io/npm/l/zod-express-guard?style=flat-square)]()\n\n\u003c!-- all-shields/badges:END --\u003e\n\u003c!-- all-shields/engines:START --\u003e\n\n[![](https://img.shields.io/node/v-lts/zod-express-guard?style=flat-square\u0026logo=node.js\u0026logoColor=white\u0026color=339933)]()\n\n\u003c!-- all-shields/engines:END --\u003e\n\u003c!-- all-shields/dependencies:START --\u003e\n\n[![](https://img.shields.io/npm/dependency-version/zod-express-guard/peer/zod?style=flat-square)]() [![](https://img.shields.io/npm/dependency-version/zod-express-guard/peer/express?style=flat-square)]() [![](https://img.shields.io/npm/dependency-version/zod-express-guard/peer/@types/express?style=flat-square)]()\n\n\u003c!-- all-shields/dependencies:END --\u003e\n\n---\n\n## Installation\n\nJust install with your favorite package manager:\n\n`npm i zod-express-guard`\n\nZod, Express and @types/express are peer dependencies, so you can change versions without having to\nwait for me to update this library. Newer versions of npm resolve these by default, but you can do\nit manually like this:\n\n`npm i -D @types/express \u0026\u0026 npm i express zod`\n\n## Usage\n\nThis library exposes four validation functions, and three of them are shortcuts that end up calling\nthe first one.\n\nAll functions support async middlewares and will catch async errors.\n\n### validate\n\nThis is the main function. It receives an object with the Zod schemas for what you want to validate,\ntogether with the middleware you want to guard. If validation passes, your middleware will be called\nwith type-safe `req` properties. If validation fails, `next` will be called containg and instance of\n`ZodError`\n\n**Example**:\n\n```typescript\n// routes/login.ts\nimport z from 'zod'\nimport { validate } from 'zod-express-guard'\n\nexport default validate(\n  {\n    body: z.object({\n      login: z.string().nonempty(),\n      password: z.string().nonempty()\n    })\n  },\n  (req, res) =\u003e {\n    // req.body has the type { login: string, password: string }\n    // req.query and req.params have type unknown, since they were not validated\n    res.status(200).json({ message: 'Validation passed' })\n  }\n)\n```\n\nYou can pass up to three properties to the first parameter: `body`, `query` and `params`, each one\nvalidates its respective property inside `req`\n\n### `validateBody`, `validateQuery` and `validateParams`\n\nThese three functions serve as shortcuts when you only want to validate a single property inside\n`req`. This is useful for our login middleware in the previous example:\n\n```typescript\n// routes/login.ts\n\nimport z from 'zod'\nimport { validateBody } from 'zod-express-guard'\n\nexport default validateBody(\n  z.object({\n    login: z.string().nonempty(),\n    password: z.string().nonempty()\n  }),\n  (req, res) =\u003e {\n    // req.body has the type { login: string, password: string }\n    // req.query and req.params have type unknown, since they were not validated\n    res.status(200).json({ message: 'Validation passed' })\n  }\n)\n```\n\n`validateQuery` and `validateParams` work similarly, but for `req.query` and `req.params`,\nrespectively.\n\nNote that, when using a shortcut function, only one of the three properties will be validated and\nmade acessible, and the other two will have type `unknown`\n\n## Contributing\n\n### Environment setup\n\nClone this and run `npm install` on the root.\n\n### Making changes\n\nPlease make sure you modify the tests to reflect any changes you add.\n\nUse `npx gitmoji -c` to commit, and follow existing commit patterns\n\n### Running tests\n\nSimply run `npm test` after running `npm install`.\n\nThe `package.json` already specifies a `pretest` script that will run a clean build before testing.\n","funding_links":[],"categories":["APIs and Servers"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froziscoding%2Fzod-express-guard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Froziscoding%2Fzod-express-guard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froziscoding%2Fzod-express-guard/lists"}