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

Using refine() and transform together for an object #238

Closed
yusukebe opened this issue Jun 14, 2024 · 3 comments · Fixed by #240
Closed

Using refine() and transform together for an object #238

yusukebe opened this issue Jun 14, 2024 · 3 comments · Fixed by #240

Comments

@yusukebe
Copy link

Hi, thank you for such a great library!

This is like a question but may be a bug report. Can we use refine() and transform() together for an object? The following code will throw TypeScript type errors:

import { OpenAPIRegistry, extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi'
import { z } from '@hono/zod-openapi'

extendZodWithOpenApi(z)

const user = z
  .object({ name: z.string(), age: z.string() })
  .refine((val) => val.name || val.age, {
    message: 'name or age required',
    path: ['name', 'age']
  })
  .transform((data) => data) // <--- Add .transform()

const registry = new OpenAPIRegistry()

registry.registerPath({
  method: 'get',
  path: '/users/{id}',
  summary: 'Get a single user',
  request: {
    params: user, // <-- error
    query: user, // <-- error
    body: {
      content: {
        'application/json': {
          schema: user
        }
      }
    }
  },
  responses: {}
})

This is related to the honojs/middleware#560

@AGalabov
Copy link
Collaborator

@yusukebe - yes this is something that we could try and fix. If you want to open a PR - feel free to do so. If not - I will try and take a look at it in the near future.

@AGalabov
Copy link
Collaborator

@yusukebe it turned out that it was a very easy fix. This is now released as part of our v7.1.0 Release

@yusukebe
Copy link
Author

@AGalabov Wow quick. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants