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