Security News
New Python Packaging Proposal Aims to Solve Phantom Dependency Problem with SBOMs
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
@hono/zod-openapi
Advanced tools
Zod OpenAPI Hono is extending Hono to support OpenAPI. With it, you can validate values and types using Zod and generate OpenAPI Swagger documentation. This is based on Zod to OpenAPI. For details on creating schemas and defining routes, please refer to this resource.
This is not a real middleware but hosted on this monorepo.
You can install it via the npm. Should be installed with hono
and zod
.
npm i hono zod @hono/zod-openapi
First, define schemas with Zod:
import { z } from '@hono/zod-openapi'
const ParamsSchema = z.object({
id: z
.string()
.min(3)
.openapi({
param: {
name: 'id',
in: 'path',
},
example: '1212121',
}),
})
const UserSchema = z
.object({
id: z.string().openapi({
example: 123,
}),
name: z.string().openapi({
example: 'John Doe',
}),
age: z.number().openapi({
example: 42,
}),
})
.openapi('User')
Next, create routes:
import { createRoute } from '@hono/zod-openapi'
const route = createRoute({
method: 'get',
path: '/users/:id',
request: {
params: ParamsSchema,
},
responses: {
200: {
content: {
'application/json': {
schema: UserSchema,
},
},
description: 'Get the user',
},
},
})
Finally, create the App:
import { OpenAPIHono } from '@hono/zod-openapi'
const app = new OpenAPIHono()
app.openapi(route, (c) => {
const { id } = c.req.valid('param')
return c.jsonT({
id,
age: 20,
name: 'Ultra-man',
})
})
// OpenAPI document will be served on /doc
app.doc('/doc', {
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'My API',
},
})
You can handle the validation errors the following ways.
Define the schema:
const ErrorSchema = z.object({
code: z.number().openapi({
example: 400,
}),
message: z.string().openapi({
example: 'Bad Request',
}),
})
Add the response:
const route = createRoute({
method: 'get',
path: '/users/:id',
request: {
params: ParamsSchema,
},
responses: {
400: {
content: {
'application/json': {
schema: ErrorSchema,
},
},
description: 'Return Error!',
},
},
})
Add the hook:
app.openapi(
route,
(c) => {
const { id } = c.req.valid('param')
return c.jsonT({
id: Number(id),
age: 20,
name: 'Ultra-man',
})
},
// Hook
(result, c) => {
if (!result.success) {
return c.jsonT(
{
code: 400,
message: 'Validation Error!',
},
400
)
}
}
)
You can use Hono's middleware as same as using Hono because Zod OpenAPI is just extending Hono.
import { prettyJSON } from 'hono/pretty-json'
//...
app.use('/doc/*', prettyJSON())
Zod OpenAPI Hono supports Hono's RPC-mode. You can create the types for passing Hono Client:
import { hc } from 'hono/client'
const appRoutes = app.openapi(route, (c) => {
const data = c.req.valid('json')
return c.jsonT({
id: data.id,
message: 'Success',
})
})
const client = hc<typeof appRoutes>('http://localhost:8787/')
MIT
FAQs
A wrapper class of Hono which supports OpenAPI.
The npm package @hono/zod-openapi receives a total of 48,629 weekly downloads. As such, @hono/zod-openapi popularity was classified as popular.
We found that @hono/zod-openapi 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
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
Security News
Socket CEO Feross Aboukhadijeh discusses open source security challenges, including zero-day attacks and supply chain risks, on the Cyber Security Council podcast.
Security News
Research
Socket researchers uncover how threat actors weaponize Out-of-Band Application Security Testing (OAST) techniques across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.