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

[Bug] zod circular dependency #1029

Open
csaba-veezla opened this issue Jun 6, 2024 · 2 comments
Open

[Bug] zod circular dependency #1029

csaba-veezla opened this issue Jun 6, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@csaba-veezla
Copy link

csaba-veezla commented Jun 6, 2024

What version of kubb is running?

2.19.5

What platform is your computer?

MacOS

What version of external packages are you using(@tanstack-query, MSW, React, Vue, ...)

No response

What steps can reproduce the bug?

We have a circular dependency in our schema, like Category can have a children with Category array.
I think, this is a fairly common scenario or at least something that should be handled properly.
The typescript infer fails on this, resulting in a build fail caused by the implicit any.
Here is how it should be handled:
https://zod.dev/?id=recursive-types
currently this is the generated code:

export const ctegorySchema = z.object({ 'id': z.string(), 'label': z.string(), 'children': z.array(z.lazy(() => categorySchema)).nullable().nullish() });

This might have some effect on #1011 as well.

How often does this bug happen?

Every time

What is the expected behavior?

The generated code should be:

export const ctegorySchema: z.ZodSchema<Category> = z.object({ 'id': z.string(), 'label': z.string(), 'children': z.array(z.lazy(() => categorySchema)).nullable().nullish() });

Where the type in z.ZodSchema is the one that the ts plugin generates.

Swagger/OpenAPI file?

No response

Additional information

No response

@csaba-veezla csaba-veezla added the bug Something isn't working label Jun 6, 2024
@stijnvanhulle
Copy link
Collaborator

@csaba-veezla Would https://www.kubb.dev/plugins/swagger-zod/#typed help you out?
Example Code

import { z } from 'zod'
import type { Tag } from '../ts/Tag'

export const tagSchema = z.object({ id: z.coerce.number().optional(), name: z.coerce.string().optional() }) as z.ZodType<Tag>

@csaba-veezla
Copy link
Author

@stijnvanhulle
I missed that option, thanks, given the example, I guess it will.
I'll check and get back to you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants