fix(standard-validator): fix hook's result type (#964)
* fix standard validator type * add test and format code --------- Co-authored-by: Yusuke Wada <yusuke@kamawada.com>pull/967/head
parent
0ebb6854fc
commit
352507bc06
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@hono/standard-validator': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix result type
|
|
@ -1,6 +1,6 @@
|
||||||
|
import type { StandardSchemaV1 } from '@standard-schema/spec'
|
||||||
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 type { StandardSchemaV1 } from '@standard-schema/spec'
|
|
||||||
|
|
||||||
type HasUndefined<T> = undefined extends T ? true : false
|
type HasUndefined<T> = undefined extends T ? true : false
|
||||||
type TOrPromiseOfT<T> = T | Promise<T>
|
type TOrPromiseOfT<T> = T | Promise<T>
|
||||||
|
@ -13,8 +13,8 @@ type Hook<
|
||||||
O = {}
|
O = {}
|
||||||
> = (
|
> = (
|
||||||
result: (
|
result: (
|
||||||
| { success: boolean; data: T }
|
| { success: true; data: T }
|
||||||
| { success: boolean; error: ReadonlyArray<StandardSchemaV1.Issue>; data: T }
|
| { success: false; error: ReadonlyArray<StandardSchemaV1.Issue>; data: T }
|
||||||
) & {
|
) & {
|
||||||
target: Target
|
target: Target
|
||||||
},
|
},
|
||||||
|
@ -54,7 +54,7 @@ const sValidator = <
|
||||||
|
|
||||||
if (hook) {
|
if (hook) {
|
||||||
const hookResult = await hook(
|
const hookResult = await hook(
|
||||||
!!result.issues
|
result.issues
|
||||||
? { data: value, error: result.issues, success: false, target }
|
? { data: value, error: result.issues, success: false, target }
|
||||||
: { data: value, success: true, target },
|
: { data: value, success: true, target },
|
||||||
c
|
c
|
||||||
|
|
|
@ -23,6 +23,7 @@ const queryPaginationSchema = type({
|
||||||
})
|
})
|
||||||
|
|
||||||
const querySortSchema = type({
|
const querySortSchema = type({
|
||||||
|
// eslint-disable-next-line quotes
|
||||||
order: "'asc'|'desc'",
|
order: "'asc'|'desc'",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
|
import type { StandardSchemaV1 } from '@standard-schema/spec'
|
||||||
import { Hono } from 'hono'
|
import { Hono } from 'hono'
|
||||||
import type { Equal, Expect, UnionToIntersection } from 'hono/utils/types'
|
import type { Equal, Expect, UnionToIntersection } from 'hono/utils/types'
|
||||||
import { sValidator } from '../src'
|
|
||||||
import { vi } from 'vitest'
|
import { vi } from 'vitest'
|
||||||
|
import { sValidator } from '../src'
|
||||||
|
|
||||||
|
import * as arktypeSchemas from './__schemas__/arktype'
|
||||||
import * as valibotSchemas from './__schemas__/valibot'
|
import * as valibotSchemas from './__schemas__/valibot'
|
||||||
import * as zodSchemas from './__schemas__/zod'
|
import * as zodSchemas from './__schemas__/zod'
|
||||||
import * as arktypeSchemas from './__schemas__/arktype'
|
|
||||||
|
|
||||||
type ExtractSchema<T> = T extends Hono<infer _, infer S> ? S : never
|
type ExtractSchema<T> = T extends Hono<infer _, infer S> ? S : never
|
||||||
type MergeDiscriminatedUnion<U> = UnionToIntersection<U> extends infer O
|
type MergeDiscriminatedUnion<U> = UnionToIntersection<U> extends infer O
|
||||||
|
@ -183,6 +184,9 @@ describe('Standard Schema Validation', () => {
|
||||||
'/post',
|
'/post',
|
||||||
sValidator('json', schema, (result, c) => {
|
sValidator('json', schema, (result, c) => {
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
|
type verify = Expect<
|
||||||
|
Equal<ReadonlyArray<StandardSchemaV1.Issue>, typeof result.error>
|
||||||
|
>
|
||||||
return c.text(`${result.data.id} is invalid!`, 400)
|
return c.text(`${result.data.id} is invalid!`, 400)
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
Loading…
Reference in New Issue