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
paihu 2025-02-10 18:42:04 +09:00 committed by GitHub
parent 0ebb6854fc
commit 352507bc06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 6 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/standard-validator': patch
---
fix result type

View File

@ -1,6 +1,6 @@
import type { StandardSchemaV1 } from '@standard-schema/spec'
import type { Context, Env, Input, MiddlewareHandler, TypedResponse, ValidationTargets } from 'hono'
import { validator } from 'hono/validator'
import type { StandardSchemaV1 } from '@standard-schema/spec'
type HasUndefined<T> = undefined extends T ? true : false
type TOrPromiseOfT<T> = T | Promise<T>
@ -13,8 +13,8 @@ type Hook<
O = {}
> = (
result: (
| { success: boolean; data: T }
| { success: boolean; error: ReadonlyArray<StandardSchemaV1.Issue>; data: T }
| { success: true; data: T }
| { success: false; error: ReadonlyArray<StandardSchemaV1.Issue>; data: T }
) & {
target: Target
},
@ -54,7 +54,7 @@ const sValidator = <
if (hook) {
const hookResult = await hook(
!!result.issues
result.issues
? { data: value, error: result.issues, success: false, target }
: { data: value, success: true, target },
c

View File

@ -23,6 +23,7 @@ const queryPaginationSchema = type({
})
const querySortSchema = type({
// eslint-disable-next-line quotes
order: "'asc'|'desc'",
})

View File

@ -1,11 +1,12 @@
import type { StandardSchemaV1 } from '@standard-schema/spec'
import { Hono } from 'hono'
import type { Equal, Expect, UnionToIntersection } from 'hono/utils/types'
import { sValidator } from '../src'
import { vi } from 'vitest'
import { sValidator } from '../src'
import * as arktypeSchemas from './__schemas__/arktype'
import * as valibotSchemas from './__schemas__/valibot'
import * as zodSchemas from './__schemas__/zod'
import * as arktypeSchemas from './__schemas__/arktype'
type ExtractSchema<T> = T extends Hono<infer _, infer S> ? S : never
type MergeDiscriminatedUnion<U> = UnionToIntersection<U> extends infer O
@ -183,6 +184,9 @@ describe('Standard Schema Validation', () => {
'/post',
sValidator('json', schema, (result, c) => {
if (!result.success) {
type verify = Expect<
Equal<ReadonlyArray<StandardSchemaV1.Issue>, typeof result.error>
>
return c.text(`${result.data.id} is invalid!`, 400)
}
}),