Skip to content

using .optional() on query param schema using zodValidator resolves enums to strings #4584

@alonzuman

Description

@alonzuman

What version of Hono are you using?

4.11.1

What runtime/platform is your app running on? (with version if possible)

Bun

What steps can reproduce the bug?

const app = new Hono<AppEnv>()

const feedQueryParamsSchema = z.object({
  userId: z.string().optional(),
  remixId: z.string().optional(),
  visibility: z.array(z.enum(['public', 'private', 'draft'])).optional(),
  orderBy: z.enum(['published-at', 'updated-at', 'trending']).optional(),
  cursor: z.string().optional(),
  limit: z.coerce.number().min(1).max(100).optional().default(6),
})

const apps = app.get('/feed', zValidator('query', feedQueryParamsSchema), async (c) => {
  const { cursor, limit } = c.req.valid('query')
  
  return c.json({
    data: [],
    nextCursor: 'some-string',
  })
})

export { apps }

export type AppsRoute = typeof apps

This resolves to

Image

If I remove the .optional(), it works well

Image

What is the expected behavior?

.optional() should not affect the type inference

What do you see instead?

I'm seeing fields being resolved to string | string [] | undefined, and not their actual type

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions