feat(zod-openapi): support Zod v4
parent
44b1c24b95
commit
bdd25f2056
|
@ -40,7 +40,7 @@
|
||||||
"homepage": "https://github.com/honojs/middleware",
|
"homepage": "https://github.com/honojs/middleware",
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"hono": ">=4.3.6",
|
"hono": ">=4.3.6",
|
||||||
"zod": "3.*"
|
"zod": "^3.25.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@arethetypeswrong/cli": "^0.17.4",
|
"@arethetypeswrong/cli": "^0.17.4",
|
||||||
|
@ -49,10 +49,10 @@
|
||||||
"typescript": "^5.8.2",
|
"typescript": "^5.8.2",
|
||||||
"vitest": "^3.0.8",
|
"vitest": "^3.0.8",
|
||||||
"yaml": "^2.4.3",
|
"yaml": "^2.4.3",
|
||||||
"zod": "^3.22.1"
|
"zod": "^3.25.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@asteasolutions/zod-to-openapi": "^7.3.0",
|
"@asteasolutions/zod-to-openapi": "^8.0.0-beta.3",
|
||||||
"@hono/zod-validator": "workspace:^"
|
"@hono/zod-validator": "workspace:^"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|
|
@ -240,7 +240,7 @@ describe('coerce', () => {
|
||||||
type Actual = ExtractSchema<typeof routes>['/api/users/:id']['$get']['input']
|
type Actual = ExtractSchema<typeof routes>['/api/users/:id']['$get']['input']
|
||||||
type Expected = {
|
type Expected = {
|
||||||
param: {
|
param: {
|
||||||
id: number
|
id: unknown
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
type verify = Expect<Equal<Expected, Actual>>
|
type verify = Expect<Equal<Expected, Actual>>
|
||||||
|
|
|
@ -11,6 +11,7 @@ import {
|
||||||
OpenApiGeneratorV3,
|
OpenApiGeneratorV3,
|
||||||
OpenApiGeneratorV31,
|
OpenApiGeneratorV31,
|
||||||
extendZodWithOpenApi,
|
extendZodWithOpenApi,
|
||||||
|
getOpenApiMetadata,
|
||||||
} from '@asteasolutions/zod-to-openapi'
|
} from '@asteasolutions/zod-to-openapi'
|
||||||
import { zValidator } from '@hono/zod-validator'
|
import { zValidator } from '@hono/zod-validator'
|
||||||
import { Hono } from 'hono'
|
import { Hono } from 'hono'
|
||||||
|
@ -36,8 +37,8 @@ import type {
|
||||||
} from 'hono/utils/http-status'
|
} from 'hono/utils/http-status'
|
||||||
import type { JSONParsed, JSONValue, RemoveBlankRecord, SimplifyDeepArray } from 'hono/utils/types'
|
import type { JSONParsed, JSONValue, RemoveBlankRecord, SimplifyDeepArray } from 'hono/utils/types'
|
||||||
import { mergePath } from 'hono/utils/url'
|
import { mergePath } from 'hono/utils/url'
|
||||||
import type { ZodError, ZodSchema } from 'zod'
|
import type { ZodError } from 'zod/v4'
|
||||||
import { ZodType, z } from 'zod'
|
import { ZodType, z } from 'zod/v4'
|
||||||
|
|
||||||
type MaybePromise<T> = Promise<T> | T
|
type MaybePromise<T> = Promise<T> | T
|
||||||
|
|
||||||
|
@ -126,7 +127,7 @@ type InputTypeJson<R extends RouteConfig> = R['request'] extends RequestTypes
|
||||||
? {}
|
? {}
|
||||||
: R['request']['body']['content'][keyof R['request']['body']['content']] extends Record<
|
: R['request']['body']['content'][keyof R['request']['body']['content']] extends Record<
|
||||||
'schema',
|
'schema',
|
||||||
ZodSchema<any>
|
ZodType<any>
|
||||||
>
|
>
|
||||||
? {
|
? {
|
||||||
in: {
|
in: {
|
||||||
|
@ -152,7 +153,7 @@ type InputTypeForm<R extends RouteConfig> = R['request'] extends RequestTypes
|
||||||
? {}
|
? {}
|
||||||
: R['request']['body']['content'][keyof R['request']['body']['content']] extends Record<
|
: R['request']['body']['content'][keyof R['request']['body']['content']] extends Record<
|
||||||
'schema',
|
'schema',
|
||||||
ZodSchema<any>
|
ZodType<any>
|
||||||
>
|
>
|
||||||
? {
|
? {
|
||||||
in: {
|
in: {
|
||||||
|
@ -179,7 +180,7 @@ type InputTypeCookie<R extends RouteConfig> = InputTypeBase<R, 'cookies', 'cooki
|
||||||
type ExtractContent<T> = T extends {
|
type ExtractContent<T> = T extends {
|
||||||
[K in keyof T]: infer A
|
[K in keyof T]: infer A
|
||||||
}
|
}
|
||||||
? A extends Record<'schema', ZodSchema>
|
? A extends Record<'schema', ZodType>
|
||||||
? z.infer<A['schema']>
|
? z.infer<A['schema']>
|
||||||
: never
|
: never
|
||||||
: never
|
: never
|
||||||
|
@ -520,6 +521,7 @@ export class OpenAPIHono<
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isFormContentType(mediaType)) {
|
if (isFormContentType(mediaType)) {
|
||||||
|
// @ts-expect-error The current zValidator does not accept ZodType
|
||||||
const validator = zValidator('form', schema, hook as any)
|
const validator = zValidator('form', schema, hook as any)
|
||||||
if (route.request?.body?.required) {
|
if (route.request?.body?.required) {
|
||||||
validators.push(validator)
|
validators.push(validator)
|
||||||
|
@ -657,11 +659,14 @@ export class OpenAPIHono<
|
||||||
})
|
})
|
||||||
|
|
||||||
case 'schema':
|
case 'schema':
|
||||||
return this.openAPIRegistry.register(def.schema._def.openapi._internal.refId, def.schema)
|
return this.openAPIRegistry.register(
|
||||||
|
getOpenApiMetadata(def.schema)._internal?.refId,
|
||||||
|
def.schema
|
||||||
|
)
|
||||||
|
|
||||||
case 'parameter':
|
case 'parameter':
|
||||||
return this.openAPIRegistry.registerParameter(
|
return this.openAPIRegistry.registerParameter(
|
||||||
def.schema._def.openapi._internal.refId,
|
getOpenApiMetadata(def.schema)._internal?.refId,
|
||||||
def.schema
|
def.schema
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
25
yarn.lock
25
yarn.lock
|
@ -99,14 +99,14 @@ __metadata:
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"@asteasolutions/zod-to-openapi@npm:^7.3.0":
|
"@asteasolutions/zod-to-openapi@npm:^8.0.0-beta.3":
|
||||||
version: 7.3.0
|
version: 8.0.0-beta.3
|
||||||
resolution: "@asteasolutions/zod-to-openapi@npm:7.3.0"
|
resolution: "@asteasolutions/zod-to-openapi@npm:8.0.0-beta.3"
|
||||||
dependencies:
|
dependencies:
|
||||||
openapi3-ts: "npm:^4.1.2"
|
openapi3-ts: "npm:^4.1.2"
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
zod: ^3.20.2
|
zod: ~3.25.1
|
||||||
checksum: 10c0/f0a68a89929cdeaa3e21d2027489689f982824d676a9332c680e119f60881dd39b571324b24ad4837fda49bf6fe7c3e2af2199268b281bf1aec923d7a7cbfc40
|
checksum: 10c0/73e9554ca4650bd7daa2cb4a66763e81e6d60a39c429db206e5c4bbd081ffbe6fac998b50a7b1e8ded4ca71b9629fafb8eadce4588d7fad89c1177869b2a35b7
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
|
@ -2379,17 +2379,17 @@ __metadata:
|
||||||
resolution: "@hono/zod-openapi@workspace:packages/zod-openapi"
|
resolution: "@hono/zod-openapi@workspace:packages/zod-openapi"
|
||||||
dependencies:
|
dependencies:
|
||||||
"@arethetypeswrong/cli": "npm:^0.17.4"
|
"@arethetypeswrong/cli": "npm:^0.17.4"
|
||||||
"@asteasolutions/zod-to-openapi": "npm:^7.3.0"
|
"@asteasolutions/zod-to-openapi": "npm:^8.0.0-beta.3"
|
||||||
"@hono/zod-validator": "workspace:^"
|
"@hono/zod-validator": "workspace:^"
|
||||||
publint: "npm:^0.3.9"
|
publint: "npm:^0.3.9"
|
||||||
tsup: "npm:^8.4.0"
|
tsup: "npm:^8.4.0"
|
||||||
typescript: "npm:^5.8.2"
|
typescript: "npm:^5.8.2"
|
||||||
vitest: "npm:^3.0.8"
|
vitest: "npm:^3.0.8"
|
||||||
yaml: "npm:^2.4.3"
|
yaml: "npm:^2.4.3"
|
||||||
zod: "npm:^3.22.1"
|
zod: "npm:^3.25.0"
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
hono: ">=4.3.6"
|
hono: ">=4.3.6"
|
||||||
zod: 3.*
|
zod: ^3.25.0
|
||||||
languageName: unknown
|
languageName: unknown
|
||||||
linkType: soft
|
linkType: soft
|
||||||
|
|
||||||
|
@ -15153,7 +15153,7 @@ __metadata:
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"zod@npm:^3.20.2, zod@npm:^3.22.1, zod@npm:^3.22.3":
|
"zod@npm:^3.20.2, zod@npm:^3.22.3":
|
||||||
version: 3.24.2
|
version: 3.24.2
|
||||||
resolution: "zod@npm:3.24.2"
|
resolution: "zod@npm:3.24.2"
|
||||||
checksum: 10c0/c638c7220150847f13ad90635b3e7d0321b36cce36f3fc6050ed960689594c949c326dfe2c6fa87c14b126ee5d370ccdebd6efb304f41ef5557a4aaca2824565
|
checksum: 10c0/c638c7220150847f13ad90635b3e7d0321b36cce36f3fc6050ed960689594c949c326dfe2c6fa87c14b126ee5d370ccdebd6efb304f41ef5557a4aaca2824565
|
||||||
|
@ -15174,6 +15174,13 @@ __metadata:
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
|
"zod@npm:^3.25.0":
|
||||||
|
version: 3.25.64
|
||||||
|
resolution: "zod@npm:3.25.64"
|
||||||
|
checksum: 10c0/00d76093a999e377e4ffd037fa7185e861c35917e8c4272f514115c206a0654995168f57fb71708b11e0a9243206d988b7f63b543404e1796402e50d346a6bd7
|
||||||
|
languageName: node
|
||||||
|
linkType: hard
|
||||||
|
|
||||||
"zod@npm:~3.25.6":
|
"zod@npm:~3.25.6":
|
||||||
version: 3.25.36
|
version: 3.25.36
|
||||||
resolution: "zod@npm:3.25.36"
|
resolution: "zod@npm:3.25.36"
|
||||||
|
|
Loading…
Reference in New Issue