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

Generate schema from types within one file #219

Closed
kirillkurko opened this issue Apr 2, 2024 · 5 comments · Fixed by #221
Closed

Generate schema from types within one file #219

kirillkurko opened this issue Apr 2, 2024 · 5 comments · Fixed by #221
Labels
bug Something isn't working

Comments

@kirillkurko
Copy link

Bug description

In versions 3.6.1 and below (tested down to 3.1.2), it was possible to generate zod schemas from types within a single file using a command similar to the one below:

# Note that the input file matches the output file
npx ts-to-zod some-file.ts some-file.ts

After updating to version 3.7.3 (3.7.0, 3.7.1 and 3.7.2 have not been tested), the above command results in an error and no zod schemas are generated. The same problem occurs when using version 3.8.0.

Input

  1. Create some file with types, e.g. some-file.ts.
  2. Try generating zod schemas from types in some-file.ts and output the generated schemas to the same some-file.ts.

Expected output

Zod schemas are generated.

Actual output

No zod schemas are generated.

Versions

  • Typescript: v5.2.2
  • Zod: v3.22.4
@tvillaren
Copy link
Collaborator

Hello @kirillkurko,

Thanks for reporting.

Can you confirm that it works when generating to another file that the initial one?
Can you confirm that the error you get is about validating the files? If so, would you mind trying with the --skipValidation flag on?

@kirillkurko
Copy link
Author

Hey @tvillaren, I can confirm that it works when generating to another file. And zod schemas are generated when running with the --skipValidation flag on, so yes, this has something to do with validating the files

@tvillaren tvillaren added the bug Something isn't working label Apr 4, 2024
@tvillaren
Copy link
Collaborator

Thanks for reporting, I found the issue.
I need to add test cases to cover this and prevent regression, before merging.

I commited the fix (20175f4) if you need to use it or would like to try it and confirm it does fix your issue 🙏

@kirillkurko
Copy link
Author

Sounds great, thank you for such a quick response and finding a solution! I'll validate the fix tomorrow and let you know if it works

@kirillkurko
Copy link
Author

Hi @tvillaren, sorry for the late reply, but I just checked the fix and it solved the problem, thanks! 🚀

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

Successfully merging a pull request may close this issue.

2 participants