{"_id":"@darkf0r3st/zod-express","name":"@darkf0r3st/zod-express","dist-tags":{"latest":"0.3.0"},"versions":{"0.3.0":{"name":"@darkf0r3st/zod-express","version":"0.3.0","description":"Express middleware for input validation and parsing using [Zod](https://zod.dev).","author":{"name":"Adam Bobrow","url":"https://github.com/bobrowadam"},"contributors":[{"name":"Shahar Kedar","url":"https://github.com/shaharke"}],"repository":{"type":"git","url":"git+https://github.com/darkf0r3st/zod-express.git"},"bugs":{"url":"https://github.com/darkf0r3st/zod-express/issues"},"type":"module","license":"MIT","types":"dist/index.d.ts","main":"dist/index.js","module":"dist/index.js","exports":{".":{"import":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"require":{"types":"./dist/index.d.cts","default":"./dist/index.cjs"}}},"devDependencies":{"@arethetypeswrong/cli":"0.15.2","@tsconfig/node18":"18.2.2","@types/express":"4.17.21","@types/jest":"29.5.12","@types/lodash":"4.17.0","@typescript-eslint/eslint-plugin":"7.2.0","@typescript-eslint/parser":"7.2.0","@vitest/coverage-istanbul":"1.4.0","axios":"^1.6.7","eslint":"latest","eslint-config-airbnb-base":"15.0.0","eslint-plugin-import":"2.26.0","express":"4.18.3","jest":"29.7.0","ts-jest":"29.1.2","tsup":"8.0.2","typescript":"5.4.2","typescript-eslint-language-service":"5.0.5","vitest":"1.4.0","zod":"3.22.4"},"dependencies":{"lodash":"4.17.21"},"peerDependencies":{"express":"^4.18.3","zod":"^3.0.0"},"engines":{"node":"^18.0.0 || ^20.0.0"},"keywords":["nodejs","api","http","middleware","json","express","typescript","schema","server","handler","schema-validation","zod","validation"],"scripts":{"test:unit":"vitest run --coverage","test:lint":"pnpm run lint","test:compile":"tsc --noEmit","test":"pnpm run test:unit && pnpm run test:lint && pnpm run test:compile","build":"tsup && attw --pack","lint":"eslint ."},"homepage":"https://github.com/darkf0r3st/zod-express#readme","_id":"@darkf0r3st/zod-express@0.3.0","_integrity":"sha512-rCe/8rpY2aOdWEoHCUfTrz8GSW3SP3/q+Uuq0rNIlTDedqXFyYMPnlfNWq79UurN9NDhgE4DU0sFLyHaSCA/jw==","_resolved":"/tmp/1ce6630ddb48f8cc6d036443a2ca7243/darkf0r3st-zod-express-0.3.0.tgz","_from":"file:darkf0r3st-zod-express-0.3.0.tgz","_nodeVersion":"18.19.1","_npmVersion":"10.2.4","dist":{"integrity":"sha512-rCe/8rpY2aOdWEoHCUfTrz8GSW3SP3/q+Uuq0rNIlTDedqXFyYMPnlfNWq79UurN9NDhgE4DU0sFLyHaSCA/jw==","shasum":"a5ecc47d3d223083f38429ccaaeb21a4870f44b0","tarball":"https://registry.npmjs.org/@darkf0r3st/zod-express/-/zod-express-0.3.0.tgz","fileCount":10,"unpackedSize":18648,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCABJU3r4IKpEdNoIMlV4AVDV2oEERuT8Qg047f3J3q+gIgInJYjK5qtyRGjyilFVHOSQZ/NkY1senwGR7+ybh7VOY="}]},"_npmUser":{"name":"shaharke","email":"shahar@gmail.com"},"directories":{},"maintainers":[{"name":"echoesbob","email":"adam@riseup.co.il"},{"name":"shaharke","email":"shahar@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/zod-express_0.3.0_1711782308020_0.3168387822133649"},"_hasShrinkwrap":false}},"time":{"created":"2024-03-30T07:05:07.913Z","0.3.0":"2024-03-30T07:05:08.170Z","modified":"2024-03-30T07:05:08.503Z"},"maintainers":[{"name":"echoesbob","email":"adam@riseup.co.il"},{"name":"shaharke","email":"shahar@gmail.com"}],"description":"Express middleware for input validation and parsing using [Zod](https://zod.dev).","homepage":"https://github.com/darkf0r3st/zod-express#readme","keywords":["nodejs","api","http","middleware","json","express","typescript","schema","server","handler","schema-validation","zod","validation"],"repository":{"type":"git","url":"git+https://github.com/darkf0r3st/zod-express.git"},"contributors":[{"name":"Shahar Kedar","url":"https://github.com/shaharke"}],"author":{"name":"Adam Bobrow","url":"https://github.com/bobrowadam"},"bugs":{"url":"https://github.com/darkf0r3st/zod-express/issues"},"license":"MIT","readme":"# Express Middleware with Zod Parsing\n\n![CI](https://github.com/darkf0r3st/zod-express/actions/workflows/test.yml/badge.svg)\n![Coverage Status](https://coveralls.io/repos/github/darkf0r3st/zod-express/badge.svg?branch=master)\n![npm release](https://img.shields.io/npm/v/@shaharke/zod-express.svg?color=green25&label=latest)\n![License](https://img.shields.io/npm/l/@shaharke/zod-express.svg?color=green25)\n\nThis module provides a powerful Express middleware for input validation and parsing using [Zod](https://zod.dev). \nIt simplifies the process of ensuring that your route handlers receive correctly typed and validated data, \nwhile also handling errors in a consistent manner.\n\n## Features\n\n- **Type Safety**: Leverages Zod to enforce input types, ensuring that your handlers work with data that adheres to your specifications.\n- **Error Handling**: Includes a mechanism to handle unexpected errors gracefully, as well as to report parsing errors back to the client.\n\n## Installation\n\nTo use this middleware, ensure you have Express and Zod installed in your project. If not, you can install them using npm:\n\n```zsh\nnpm install @shaharke/zod-express\n```\n\n## Usage\n\n### Basic Usage\nFor a route that requires input validation:\n\n```typescript\nimport express from 'express';\nimport { zodx } from '@shaharke/zod-express';\nimport { z } from 'zod';\n\nconst app = express();\n\napp.use(express.json()); // for parsing application/json\n\nconst InputSchema = z.object({\n  foo: z.string(),\n});\ntype Input = z.infer<typeof InputSchema>;\n\napp.post('/your-route', zodx(async (input: Input) => {\n  // Your logic here, with input being already validated\n  return { message: `Received: ${input.foo}` };\n}, InputSchema));\n\napp.listen(3000, () => console.log('Server running on port 3000'));\n\n```\n\n### Custom validation\n\nYou can also use a custom validation function to handle more complex validation logic:\n\n```typescript\nimport express from 'express';\nimport { zodx } from '@shaharke/zod-express';\nimport { z } from 'zod';\n\nconst app = express();\n\napp.use(express.json()); // for parsing application/json\n\nconst InputSchema = z.object({\n  foo: z.string(),\n});\ntype Input = z.infer<typeof InputSchema>;\n\nconst customValidation = (req: Request) => {\n  return InputSchema.safeParse(req.body);\n};\n\napp.post('/your-route', zodx(async (input: Input) => {\n  return { message: `Received: ${input.foo}` };\n}, customValidation));\n```\n\n### No validation\n\nFor a simple route that doesn't require input parsing:\n\n```typescript\nimport express from 'express';\nimport { zodx } from '@shaharke/zod-express';\nimport { z } from 'zod';\n\nconst app = express();\n\napp.use(express.json()); // for parsing application/json\n\napp.post('/your-route', zodx(async () => {\n  // Your logic here\n  return { message: 'Success!' };\n}));\n\napp.listen(3000, () => console.log('Server running on port 3000'));\n```\n\n## Error Handling\n\nTo handle unexpected errors, you can pass an optional error handler to the middleware:\n\n```typescript\nimport { Request, Response } from 'express';\n\nconst errorHandler = (error: Error, req: Request, res: Response) => {\n  console.error('Unexpected error:', error);\n  res.status(500).send('An unexpected error occurred');\n};\n\n// Use it as the third argument in zodx\napp.post('/your-route', zodx(handler, schema, errorHandler));\n```\n\n### Global Error Handler\n\nYou can configure a global error handler for all routes that use zod-express by calling\nthe `config` function:\n\n```typescript\nimport { config, zodx } from '@shaharke/zod-express';\n\nconfig({\n  errorHandler: (error: Error, req: Request, res: Response) => {\n    console.error('Unexpected error:', error);\n    res.status(503).send('An unexpected error occurred');\n  },\n});\n\napp.post('/your-route', zodx(handler, schema));\n```\n\nSetting a global error handler does not prevent you from passing a custom error handler to a specific route as described above.\n\n### Scoped Error Handler\n\nYou can also scope the default error handler to a specific middleware factory instance:\n\n```typescript\nimport { config } from '@shaharke/zod-express';\n\nconst zodx = factory({\n  errorHandler: (error: Error, req: Request, res: Response) => {\n    console.error('Unexpected error:', error);\n    res.status(503).send('An unexpected error occurred');\n  },\n});\n\napp.post('/your-route', zodx(handler, schema));\n```\n","readmeFilename":"README.md"}