Skip to content

Commit

Permalink
fix(zod-openapi): support multiple params
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe committed Sep 11, 2023
1 parent be7d4a6 commit 52b170f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export class OpenAPIHono<
}
}

this.on([route.method], route.path.replace(/\/{(.+)}/, '/:$1'), ...validators, handler)
this.on([route.method], route.path.replaceAll(/\/{(.+?)}/g, '/:$1'), ...validators, handler)
return this
}

Expand Down
40 changes: 40 additions & 0 deletions packages/zod-openapi/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,43 @@ describe('Types', () => {
expectTypeOf(appRoutes).toMatchTypeOf<H>
})
})

describe('Multi params', () => {
const ParamsSchema = z.object({
id: z.string(),
tagName: z.string(),
})

const route = createRoute({
method: 'get',
path: '/users/{id}/tags/{tagName}',
request: {
params: ParamsSchema,
},
responses: {
200: {
// eslint-disable-next-line quotes
description: "Get the user's tag",
},
},
})

const app = new OpenAPIHono()

app.openapi(route, (c) => {
const { id, tagName } = c.req.valid('param')
return c.jsonT({
id,
tagName,
})
})

it('Should return 200 response with correct contents', async () => {
const res = await app.request('/users/123/tags/baseball')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
id: '123',
tagName: 'baseball',
})
})
})
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2020",
"target": "ES2022",
"module": "commonjs",
"declaration": true,
"moduleResolution": "Node",
Expand Down

0 comments on commit 52b170f

Please sign in to comment.