Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(standard-validator): fix hook's result type #964

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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