{"id":27729,"library":"express-zod-validations","title":"express-zod-validations","description":"Express middleware for validating request headers, params, query, and body using Zod schemas. Current stable version 0.1.1, released as the sole version to date. It offers granular validation (each request part independently), async validation by default via safeParseAsync, and flexible error handling (store errors on req or throw to Express error handler). Ships TypeScript declarations. Requires peer dependencies express ^5 and zod ^4, which are breaking changes from express v4/zod v3 ecosystem. Differentiates from express-zod-safe by requiring explicit version compatibility and providing a middleware factory for global configuration.","status":"active","version":"0.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/mattiamalonni/express-zod-validations","tags":["javascript","express-middleware","validation-middleware","zod-validation","request-validation","input-validation","data-validations","schema-validation","input-sanitization","typescript"],"install":[{"cmd":"npm install express-zod-validations","lang":"bash","label":"npm"},{"cmd":"yarn add express-zod-validations","lang":"bash","label":"yarn"},{"cmd":"pnpm add express-zod-validations","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency: provides request/response objects for middleware injection","package":"express","optional":false},{"reason":"Peer dependency: used for schema definition and validation","package":"zod","optional":false}],"imports":[{"note":"Named export, not default","wrong":"import validateBody from 'express-zod-validations'","symbol":"validateBody","correct":"import { validateBody } from 'express-zod-validations'"},{"note":"ESM-only package; CommonJS require may not work without bundler","wrong":"const validate = require('express-zod-validations').validate","symbol":"validate","correct":"import { validate } from 'express-zod-validations'"},{"note":"Global config factory, not from subpath","wrong":"import { expressZodValidations } from 'express-zod-validations/middleware'","symbol":"expressZodValidations","correct":"import { expressZodValidations } from 'express-zod-validations'"},{"note":"Named export for params validation","wrong":null,"symbol":"validateParams","correct":"import { validateParams } from 'express-zod-validations'"},{"note":"Named export for query validation","wrong":null,"symbol":"validateQuery","correct":"import { validateQuery } from 'express-zod-validations'"},{"note":"Named export for headers validation","wrong":null,"symbol":"validateHeaders","correct":"import { validateHeaders } from 'express-zod-validations'"}],"quickstart":{"code":"import express from 'express';\nimport { z } from 'zod';\nimport { validateBody } from 'express-zod-validations';\n\nconst app = express();\napp.use(express.json());\n\nconst userSchema = z.object({\n  name: z.string().min(2),\n  email: z.string().email(),\n  age: z.number().int().positive().optional(),\n});\n\napp.post('/users', validateBody(userSchema), (req, res) => {\n  const user = req.validationValues.body;\n  if (req.validationErrors?.body) {\n    return res.status(400).json({ errors: req.validationErrors.body.errors });\n  }\n  res.json({ message: 'User created', user });\n});\n\napp.listen(3000);","lang":"typescript","description":"Sets up an Express server with Zod body validation middleware using validateBody, checks for errors stored on req, and responds accordingly."},"warnings":[{"fix":"Use Express 5 exclusively; do not use with Express 4.","message":"Requires Express v5 (peer dependency express ^5). Express 5 has breaking changes compared to Express 4 (e.g., async error handling, middleware signatures).","severity":"breaking","affected_versions":">=0.0.0"},{"fix":"Upgrade to Zod 4 and adjust schemas per Zod 4 migration guide.","message":"Requires Zod v4 (peer dependency zod ^4). Zod 4 introduces breaking changes from Zod 3 (e.g., new API for coerce, nullable, etc.).","severity":"breaking","affected_versions":">=0.0.0"},{"fix":"","message":"No deprecated features reported in this early version.","severity":"deprecated","affected_versions":""},{"fix":"Ensure an Express error handler is registered when throwErrors is true, or use default false and check req.validationErrors manually.","message":"By default, throwErrors is false and validation errors are stored in req.validationErrors. If you use throwErrors: true, errors are passed to next(error) and must be handled by Express error middleware; otherwise the request will hang.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use overwriteRequest: false (default) and access validated data via req.validationValues to preserve original request properties.","message":"When overwriteRequest is true, the original req.body, req.params, etc. are replaced with parsed values. This may bypass original type declarations.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-05-09T00:00:00.000Z","next_check":"2026-08-07T00:00:00.000Z","problems":[{"fix":"Run npm install express-zod-validations. Ensure tsconfig.json has 'moduleResolution': 'node' (or 'bundler') and 'esModuleInterop': true.","cause":"Package not installed or missing TypeScript declaration resolution.","error":"Cannot find module 'express-zod-validations' or its corresponding type declarations."},{"fix":"Ensure validateBody() (or validate) is used as middleware on the route. Do not spread middleware return value.","cause":"Middleware not applied before route handler, or middleware configuration resets validationValues.","error":"TypeError: req.validationValues is undefined"},{"fix":"Use import syntax or set type: 'module' in package.json. Alternatively, use a dynamic import: const { validateBody } = await import('express-zod-validations').","cause":"Package is ESM-only and cannot be required() in CommonJS context without transpilation.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/express-zod-validations/index.mjs not supported."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}