chore(zod-validator): format the code (#749)
parent
eda3584791
commit
950786f6d2
|
@ -2,9 +2,17 @@ import type { Context, MiddlewareHandler, Env, ValidationTargets, TypedResponse,
|
|||
import { validator } from 'hono/validator'
|
||||
import type { z, ZodSchema, ZodError } from 'zod'
|
||||
|
||||
export type Hook<T, E extends Env, P extends string, Target extends keyof ValidationTargets = keyof ValidationTargets, O = {}> = (
|
||||
result: ({ success: true; data: T} | { success: false; error: ZodError; data: T }) & {target: Target },
|
||||
c: Context<E, P>,
|
||||
export type Hook<
|
||||
T,
|
||||
E extends Env,
|
||||
P extends string,
|
||||
Target extends keyof ValidationTargets = keyof ValidationTargets,
|
||||
O = {}
|
||||
> = (
|
||||
result: ({ success: true; data: T } | { success: false; error: ZodError; data: T }) & {
|
||||
target: Target
|
||||
},
|
||||
c: Context<E, P>
|
||||
) => Response | void | TypedResponse<O> | Promise<Response | void | TypedResponse<O>>
|
||||
|
||||
type HasUndefined<T> = undefined extends T ? true : false
|
||||
|
@ -45,7 +53,7 @@ export const zValidator = <
|
|||
const result = await schema.safeParseAsync(value)
|
||||
|
||||
if (hook) {
|
||||
const hookResult = await hook({ data: value, ...result, target, }, c)
|
||||
const hookResult = await hook({ data: value, ...result, target }, c)
|
||||
if (hookResult) {
|
||||
if (hookResult instanceof Response) {
|
||||
return hookResult
|
||||
|
|
|
@ -4,7 +4,6 @@ import { z } from 'zod'
|
|||
import { zValidator } from '../src'
|
||||
import { vi } from 'vitest'
|
||||
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
type ExtractSchema<T> = T extends Hono<infer _, infer S> ? S : never
|
||||
|
||||
|
@ -259,7 +258,6 @@ describe('With Async Hook', () => {
|
|||
})
|
||||
})
|
||||
|
||||
|
||||
describe('With target', () => {
|
||||
it('should call hook for correctly validated target', async () => {
|
||||
const app = new Hono()
|
||||
|
@ -295,9 +293,17 @@ describe('With target', () => {
|
|||
expect(res).not.toBeNull()
|
||||
expect(res.status).toBe(200)
|
||||
expect(await res.text()).toBe('ok')
|
||||
expect(paramHook).toHaveBeenCalledWith({data: {id: '1'}, success: true, target:
|
||||
'param'}, expect.anything())
|
||||
expect(queryHook).toHaveBeenCalledWith({data: {id: '2'}, success: true, target: 'query'}, expect.anything())
|
||||
expect(jsonHook).toHaveBeenCalledWith({data: {id: '3'}, success: true, target: 'json'}, expect.anything())
|
||||
expect(paramHook).toHaveBeenCalledWith(
|
||||
{ data: { id: '1' }, success: true, target: 'param' },
|
||||
expect.anything()
|
||||
)
|
||||
expect(queryHook).toHaveBeenCalledWith(
|
||||
{ data: { id: '2' }, success: true, target: 'query' },
|
||||
expect.anything()
|
||||
)
|
||||
expect(jsonHook).toHaveBeenCalledWith(
|
||||
{ data: { id: '3' }, success: true, target: 'json' },
|
||||
expect.anything()
|
||||
)
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue