
Security News
CVE Volume Surges Past 48,000 in 2025 as WordPress Plugin Ecosystem Drives Growth
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.
openapi-ts-router
Advanced tools
Thin wrapper around the router of web frameworks like Express and Hono, offering OpenAPI typesafety and seamless integration with validation libraries such as Valibot and Zod
Status: Experimental
openapi-ts-router is a thin wrapper around the router of web frameworks like Express and Hono, offering OpenAPI typesafety and seamless integration with validation libraries such as Valibot and Zod.
openapi-ts-router provides full type-safety and runtime validation for your Express API routes by wrapping a Express router:
Good to Know: While TypeScript ensures compile-time type safety, runtime validation is equally important.
openapi-ts-routerintegrates with Zod/Valibot to provide both:
- Types verify your code matches the OpenAPI spec during development
- Validators ensure incoming requests match the spec at runtime
import { Router } from 'express';
import { createExpressOpenApiRouter } from 'openapi-ts-router';
import { zValidator } from 'validation-adapters/zod';
import * as z from 'zod';
import { paths } from './gen/v1'; // OpenAPI-generated types
import { PetSchema } from './schemas'; // Custom reusable schema for validation
export const router: Router = Router();
export const openApiRouter = createExpressOpenApiRouter<paths>(router);
// GET /pet/{petId}
openApiRouter.get('/pet/{petId}', {
pathValidator: zValidator(
z.object({
petId: z.number() // Validate that petId is a number
})
),
handler: (req, res) => {
const { petId } = req.valid.params; // Access parsed & validated params
res.send({ name: 'Falko', photoUrls: [] });
}
});
// POST /pet
openApiRouter.post('/pet', {
bodyValidator: zValidator(PetSchema), // Validate request body using PetSchema
handler: (req, res) => {
const { name, photoUrls } = req.body; // Access validated body data
res.send({ name, photoUrls });
}
});
// TypeScript will error if route/method doesn't exist in OpenAPI spec
// or if response doesn't match defined schema
openapi-ts-router provides full type-safety and runtime validation for your HonoAPI routes by wrapping a Hono router:
Good to Know: While TypeScript ensures compile-time type safety, runtime validation is equally important.
openapi-ts-routerintegrates with Zod/Valibot to provide both:
- Types verify your code matches the OpenAPI spec during development
- Validators ensure incoming requests match the spec at runtime
Note: Hono's TypeScript integration provides type suggestions for
c.json()based on generically defined response types, but it doesn't enforce these types at compile-time. For example,c.json('')won't raise a type error even if the expected type is{ someType: string }. This is due to Hono's internal use ofTypedResponse<T>, which infers but doesn't strictly enforce the passed generic type. Hono Discussion
import { Hono } from 'hono';
import { createHonoOpenApiRouter } from 'openapi-ts-router';
import { zValidator } from 'validation-adapters/zod';
import * as z from 'zod';
import { paths } from './gen/v1'; // OpenAPI-generated types
import { PetSchema } from './schemas'; // Custom reusable schema for validation
export const router = new Hono();
export const openApiRouter = createHonoOpenApiRouter<paths>(router);
// GET /pet/{petId}
openApiRouter.get('/pet/{petId}', {
pathValidator: zValidator(
z.object({
petId: z.number() // Validate that petId is a number
})
),
handler: (c) => {
const { petId } = c.req.valid('param'); // Access validated params
return c.json({ name: 'Falko', photoUrls: [] });
}
});
// POST /pet
openApiRouter.post('/pet', {
bodyValidator: zValidator(PetSchema), // Validate request body using PetSchema
handler: (c) => {
const { name, photoUrls } = c.req.valid('json'); // Access validated body data
return c.json({ name, photoUrls });
}
});
// TypeScript will error if route/method doesn't exist in OpenAPI spec
// or if response doesn't match defined schema
We intentionally only type success responses (2xx) while leaving error responses out. Here’s why:
Errors should be handled via exceptions & middleware
Instead of typing every possible error response inline, we believe that handling errors globally in middleware provides clearer, more maintainable code.
👉 Example: Hono Example (Docs) & Express Example (Docs)
Inline error responses require as any
Since .send() only expects success types, explicit casting is required to enforce an error response:
res.status(500).send({
code: '#ERR_XYZ',
message: 'Error Message'
} satisfies TOperationResponseContent<paths['/pet/{petId}']['get'], 500> as any);
Express and Hono don't infer status codes for res.send() / c.json()
Since we can’t infer the value of the res.status() / c.json() method call, res.send() / c.json() is typed as a union of success response types. For example, it could be:
{ message: 'Success Body of 200' } | { message: 'Success Body of 201' }
To enforce a specific success type, use satisfies
Example of explicitly enforcing a 201 response type:
res.status(201).send({
id: 123
} satisfies TOperationResponseContent<paths['/pet/{petId}']['get'], 201>);
c.json() not typesafe?Hono's TypeScript integration provides type suggestions for c.json() based on generically defined response types, but it doesn't enforce these types at compile-time. For example, c.json('') won't raise a type error even if the expected type is { someType: string }. This is due to Hono's internal use of TypedResponse<T>, which infers but doesn't strictly enforce the passed generic type. Hono Discussion
To enforce a specific success type, use satisfies
Example of explicitly enforcing a 201 response type:
c.json(
{
id: 123
} satisfies TOperationResponseContent<paths['/pet/{petId}']['get'], 201>,
201
);
FAQs
Thin wrapper around the router of web frameworks like Express and Hono, offering OpenAPI typesafety and seamless integration with validation libraries such as Valibot and Zod
We found that openapi-ts-router demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.