-
-
Notifications
You must be signed in to change notification settings - Fork 71
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
Zod: Recursive schema #769
Comments
This was fixed with |
Thank you for your answer! After updating to version 2.3.0, the code now works. 👍 However, Typescript is still complaining. 😕 After compilation it throws this error: export const itemSchema = z.object({ // <-- ts-compile complains here
name: z.string(),
children: z.array(z.lazy(() => itemSchema)) // <-- self reference to itemSchema
}); Yes, i can ignore the whole generated folder in tsconfig, but i would like it to work. I'm able to fix this error by adding type annotation to the schema like this: export const itemSchema: z.ZodType<Item> = z.object({ // <-- everything is fine now
name: z.string(),
children: z.array(z.lazy(() => itemSchema))
}); Would something like this be possible? I know it adds another plugin dependecy to zod plugin (typescript plugin), but i think it would be worth it. |
@vferak This seems more like a TypeScript config issue, maybe this related Stackoverflow issue can help: https://stackoverflow.com/questions/41944650/this-implicitly-has-type-any-because-it-does-not-have-a-type-annotation. |
The issue you linked is concerned mainly about the this keyword, which is not my issue. In tsconfig i can only ignore this file or enable The issue really stems from the fact, that Typescript can't infer the type of You can define a recursive schema in Zod, but because of a limitation of TypeScript, their type can't be statically inferred. Instead you'll need to define the type definition manually, and provide it to Zod as a "type hint". So as far as i can tell, adding the typehint seems like the only solution for now. |
Can confirm, that after upgrading to version v2.5.0 and configuring Zod with option Detailed config solving this issue is here. Thank you very much @stijnvanhulle. 🔥 |
@vferak what package did you need to upgrade to resolve the issue? v2.50 of what? I'm also experiencing this issue running the latest of kubb cc: @stijnvanhulle |
|
Thank you @stijnvanhulle I added Only thing that helping is removing .schema reference Changing this:
to
I noticed #744 (fix: removal of .schema) did this as well Why would .schema still be appended within lazy()? |
@austinlangdon yes, the .schema reappeared in our schemas, which also broked our generated code 😕. Hovewer, after upgrading to Kubb v2.19.1, they are once again gone 😀. So i recommend upgrading to fix this issue 🙂. |
What version of
kubb
is running?10.1.0
What platform is your computer?
Linux
What version of external packages are you using(
@tanstack-query
,MSW
,React
,Vue
, ...)"zod": "^3.22.2"
What steps can reproduce the bug?
When generating zod schemas from attached OpenAPI specification, it generates unusable zod schema, because it contains self reference in its inicialization.
Result of itemSchema.ts generated from attached file.
The typescript type is generated correctly.
How often does this bug happen?
Every time
What is the expected behavior?
Kubb can generate recursive Zod schemas (example of implementation).
Swagger/OpenAPI file?
Whole specification in file here: openapi.json
Troublesome part:
Additional information
No response
The text was updated successfully, but these errors were encountered: