What this thread does
Do first: enable strict: true in both TypeScript projects and validate req.body with a shared runtime schema before invoking business logic (strict, schema parsing).
Short answer
As of November 2026, the reference packages are Express 5.3.0 and Zod 4.6.7. Keep browser-safe schemas and derived DTO types in a dedicated contracts package.
Choose / don't choose
- Shared schema package: choose when the team owns both TypeScript ends and can distribute compatible contracts; don't import server startup, database clients or environment reads through its public entry.
- OpenAPI contract: choose for independent or mixed-language consumers and a governed HTTP API; generate types/client from the document, but don't mistake generated runtime-free types for runtime validation (OpenAPI TypeScript).
- Type-only client import: choose when only compile-time DTOs are needed; import runtime schemas separately when the client must parse responses. Type imports are erased (TypeScript basics). (module reference).
Not for: selecting a new API style; see rest-trpc-or-graphql-for-a-typescript-product.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
strict | Enable strict-family type checking in server, client and contracts; don't disable strict-family checks in a child config | Current TypeScript docs | strict |
noEmit | Do not emit compiler output files; use for CI type checks | Current TypeScript docs | noEmit |
declaration | Set to true in the contracts build; noEmit must not suppress the declaration-producing build | Current TypeScript docs | declaration |
emitDeclarationOnly | Only emit .d.ts files; do not emit .js files | Current TypeScript docs | emitDeclarationOnly |
package.json.exports | Example exports["./subpath"].import: types: "./types/subpath/index.d.mts", default: "./es/subpath/index.mjs"; replace with emitted browser-safe artifacts | Current package resolution | TS exports, Node packages |
types | Declaration file representing the implementation; package.json fields are tried in order: types, typings, main | Current TypeScript resolution | module reference |
strict JSON-parser option | Default true accepts arrays/objects, not a business schema; default limit '100kb'; choose a documented application limit | Current Express API | JSON parser |
safeParse | Returns successfully parsed data or a ZodError; inspect success before using data; use safeParseAsync for asynchronous refinements | Current Zod API | basics |
Example package layout: expose a schema-only entry such as @app/contracts/user, with types pointing to its emitted declaration and default to its emitted JS. These are application entry names, not special Express configuration. Check both exported artifacts exist in the packed package.
Order of operations
1. Define request and JSON response schemas once. Extract schema input and output types independently when they diverge; transforms can convert input from one type to another (Zod basics). 2. Install express.json before the route. Reject malformed JSON with the parser's error policy, then safeParse body/params/query against their actual wire shape; req.body remains untrusted even with a Request generic (request body). 3. For invalid input return the API's chosen validation status and safe field paths, with no business side effects. Pass the parsed result into the handler, authenticate/authorize there or before it as appropriate, and avoid casts that bypass the schema. 5. Client imports DTOs with import type; if it imports schemas at runtime, that dependency must remain browser-safe. Parse unknown response JSON before using it where runtime drift detection is required. Select response schema by actual status, including errors. 6. In CI build the contracts package, type-check server and client, and compile a negative incompatible DTO fixture. Exercise malformed JSON, a valid JSON object with wrong field types and a deliberately invalid server response; assert rejection before mutation/serialization. Run a client bundle inspection for server-only imports and an older-client compatibility fixture.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
entity.parse.failed | Request entity could not be parsed by the middleware; status 400 | Return a controlled parse error before schema/business handling; do not echo the raw failed body | body-parser errors |
Invalid input: expected string, received number | Zod validation failed | Handle safeParse failure; use parsed result.data on success, not a typed req.body assertion | Zod error example |
Issue #4128 reports that v4 parse and safeParse no longer accept path in params. Keep route field-path reporting based on the installed library's issue objects rather than copying older parse options.
Missing
- The linked source does not confirm: TypeScript checks assignments at build time; parsing checks the untrusted network value.
- The linked source does not confirm: Map domain data to an explicit response DTO, validate that DTO before sending res.json, and never serialize internal error objects or unrestricted database rows. Validate the JSON-compatible shape: dates and undefined are not preserved as arbitrary TypeScript objects over JSON.
- Existing routes, error status conventions, package module format and client release cadence are unknown. No universal schema-to-OpenAPI generator is selected; transforms need a separate representation policy.
Checked
2026-11-12; package releases, Express parser/request/errors, TypeScript configuration/modules/basics, Node package resolution, Zod APIs, OpenAPI generator introduction and dated issue read. No API or client build executed.
Started from a post by @Express contributors, packaged by the editor. Original post