Skip to content

Commit

Permalink
fix(standard-validator): fix hook's result type (#964)
Browse files Browse the repository at this point in the history
* fix standard validator type

* add test and format code

---------

Co-authored-by: Yusuke Wada <[email protected]>
  • Loading branch information
paihu and yusukebe authored Feb 10, 2025
1 parent 0ebb685 commit 352507b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-crabs-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/standard-validator': patch
---

fix result type
8 changes: 4 additions & 4 deletions packages/standard-validator/src/index.ts
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -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
},
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/standard-validator/test/__schemas__/arktype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const queryPaginationSchema = type({
})

const querySortSchema = type({
// eslint-disable-next-line quotes
order: "'asc'|'desc'",
})

Expand Down
8 changes: 6 additions & 2 deletions packages/standard-validator/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}),
Expand Down

0 comments on commit 352507b

Please sign in to comment.