Merge c8b2f10eee
into a756d2235b
commit
8f9cd65a3e
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@hono/zod-validator': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Added optional opt field for passthrough and might further editions, passthrough and tests to it
|
|
@ -49,10 +49,10 @@ describe('Basic', () => {
|
||||||
}
|
}
|
||||||
} & {
|
} & {
|
||||||
query?:
|
query?:
|
||||||
| {
|
| {
|
||||||
name?: string | undefined
|
name?: string | undefined
|
||||||
}
|
}
|
||||||
| undefined
|
| undefined
|
||||||
}
|
}
|
||||||
output: {
|
output: {
|
||||||
success: boolean
|
success: boolean
|
||||||
|
@ -378,3 +378,87 @@ describe('Case-Insensitive Headers', () => {
|
||||||
type verify = Expect<Equal<Expected, Actual>>
|
type verify = Expect<Equal<Expected, Actual>>
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('With options + passthrough', () => {
|
||||||
|
const app = new Hono()
|
||||||
|
const jsonSchema = z.object({
|
||||||
|
name: z.string(),
|
||||||
|
age: z.number()
|
||||||
|
});
|
||||||
|
|
||||||
|
const route = app.post(
|
||||||
|
'/',
|
||||||
|
zValidator('json', jsonSchema),
|
||||||
|
(c) => {
|
||||||
|
const data = c.req.valid('json')
|
||||||
|
|
||||||
|
return c.json({
|
||||||
|
success: true,
|
||||||
|
data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
).post('/extended',
|
||||||
|
zValidator('json', jsonSchema, undefined, {
|
||||||
|
passthroughObject: true
|
||||||
|
}),
|
||||||
|
(c) => {
|
||||||
|
const data = c.req.valid('json')
|
||||||
|
|
||||||
|
return c.json({
|
||||||
|
success: true,
|
||||||
|
data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
it('Should be ok due to passthrough schema', async () => {
|
||||||
|
const req = new Request('http://localhost/extended', {
|
||||||
|
body: JSON.stringify({
|
||||||
|
name: 'Superman',
|
||||||
|
age: 20,
|
||||||
|
length: 170,
|
||||||
|
weight: 55
|
||||||
|
}),
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
const res = await app.request(req)
|
||||||
|
expect(res).not.toBeNull()
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
expect(await res.json()).toEqual({
|
||||||
|
success: true,
|
||||||
|
data: {
|
||||||
|
name: "Superman",
|
||||||
|
age: 20,
|
||||||
|
length: 170,
|
||||||
|
weight: 55
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
it('Should be ok due to required schema', async () => {
|
||||||
|
const req = new Request('http://localhost', {
|
||||||
|
body: JSON.stringify({
|
||||||
|
name: 'Superman',
|
||||||
|
age: 20,
|
||||||
|
}),
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
const res = await app.request(req)
|
||||||
|
|
||||||
|
expect(res).not.toBeNull()
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
expect(await res.json()).toEqual({
|
||||||
|
success: true,
|
||||||
|
data: {
|
||||||
|
name: "Superman",
|
||||||
|
age: 20
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
|
@ -1,7 +1,7 @@
|
||||||
import type { Context, Env, Input, MiddlewareHandler, TypedResponse, ValidationTargets } from 'hono'
|
import type { Context, Env, Input, MiddlewareHandler, TypedResponse, ValidationTargets } from 'hono'
|
||||||
import { validator } from 'hono/validator'
|
import { validator } from 'hono/validator'
|
||||||
import { ZodObject } from 'zod'
|
import { ZodObject } from 'zod'
|
||||||
import type { ZodError, ZodSchema, z } from 'zod'
|
import { ZodError, ZodSchema, z } from 'zod'
|
||||||
|
|
||||||
export type Hook<
|
export type Hook<
|
||||||
T,
|
T,
|
||||||
|
@ -27,23 +27,24 @@ export const zValidator = <
|
||||||
Out = z.output<T>,
|
Out = z.output<T>,
|
||||||
I extends Input = {
|
I extends Input = {
|
||||||
in: HasUndefined<In> extends true
|
in: HasUndefined<In> extends true
|
||||||
? {
|
? {
|
||||||
[K in Target]?: In extends ValidationTargets[K]
|
[K in Target]?: In extends ValidationTargets[K]
|
||||||
? In
|
? In
|
||||||
: { [K2 in keyof In]?: ValidationTargets[K][K2] }
|
: { [K2 in keyof In]?: ValidationTargets[K][K2] }
|
||||||
}
|
}
|
||||||
: {
|
: {
|
||||||
[K in Target]: In extends ValidationTargets[K]
|
[K in Target]: In extends ValidationTargets[K]
|
||||||
? In
|
? In
|
||||||
: { [K2 in keyof In]: ValidationTargets[K][K2] }
|
: { [K2 in keyof In]: ValidationTargets[K][K2] }
|
||||||
}
|
}
|
||||||
out: { [K in Target]: Out }
|
out: { [K in Target]: Out }
|
||||||
},
|
},
|
||||||
V extends I = I
|
V extends I = I
|
||||||
>(
|
>(
|
||||||
target: Target,
|
target: Target,
|
||||||
schema: T,
|
schema: T,
|
||||||
hook?: Hook<z.infer<T>, E, P, Target>
|
hook?: Hook<z.infer<T>, E, P, Target>,
|
||||||
|
opt?: { passthroughObject?: boolean }
|
||||||
): MiddlewareHandler<E, P, V> =>
|
): MiddlewareHandler<E, P, V> =>
|
||||||
// @ts-expect-error not typed well
|
// @ts-expect-error not typed well
|
||||||
validator(target, async (value, c) => {
|
validator(target, async (value, c) => {
|
||||||
|
@ -63,7 +64,12 @@ export const zValidator = <
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await schema.safeParseAsync(validatorValue)
|
let result: z.infer<T>;
|
||||||
|
if (opt && "passthroughObject" in opt) {
|
||||||
|
result = await schema.passthrough().safeParseAsync(validatorValue)
|
||||||
|
} else {
|
||||||
|
result = await schema.safeParseAsync(validatorValue)
|
||||||
|
}
|
||||||
|
|
||||||
if (hook) {
|
if (hook) {
|
||||||
const hookResult = await hook({ data: validatorValue, ...result, target }, c)
|
const hookResult = await hook({ data: validatorValue, ...result, target }, c)
|
||||||
|
|
Loading…
Reference in New Issue