Skip to content

feat(config): add validationSchemaExportType option #165

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

Closed
wants to merge 0 commits into from

Conversation

elijaholmos
Copy link
Contributor

Resolves #164

Adds a new config option, validationSchemaExportType, to specify the export type of the generated validation schema (either function or const), Feature supports yup, zod, and myzod.

@Code-Hex
Copy link
Owner

@elijaholmos Hi 👋
Thanks for submitting your PR!

I think it's nice PR. I'll merge it if it is passed CI. Thanks!

@Code-Hex
Copy link
Owner

Code-Hex commented Sep 1, 2022

Hmm...
I generated with enabling this PR config and I'm getting many errors on my local PC. Could you check these errors?

スクリーンショット 2022-09-01 11 06 25

Reproduce

Run

$ yarn install --frozen-lock
$ yarn generate
$ npx tsc --noEmit example/zod/schemas.ts

@elijaholmos
Copy link
Contributor Author

Thank you for pointing this out + providing reproduction steps, @Code-Hex. I was able to get the same issue on my machine and have a few ideas for a fix. I will play around more this Friday PDT and update the PR then 👍

@Code-Hex
Copy link
Owner

Code-Hex commented Sep 1, 2022

Amazing @elijaholmos !!
I'm looking forward to your some fixes. Please mention me anytime if you need anything.

@elijaholmos
Copy link
Contributor Author

hi @Code-Hex! I've managed to resolve the enum errors that were occurring previously by updating the generateNameNodeZodSchema() method - however, after running yarn generate with the updated file, the resulting code now errors because the enums are being referenced before their initialization. see screenshot:
image

do you know if there's any way to instruct the generator to place the enum definitions above their corresponding input schemas? I tried moving the EnumTypeDefinition, but that of course had no effect 😛

@Code-Hex
Copy link
Owner

Code-Hex commented Sep 7, 2022

@elijaholmos I'm sorry for being late.
I'm not sure the way but I think you can control those orders by allocating generated functions in other places (like buffer?) as temporary.

@elijaholmos
Copy link
Contributor Author

@Code-Hex it's ok! I'll give that a try and let you know what I'm able to come up with. thanks for the starting point

@elijaholmos
Copy link
Contributor Author

elijaholmos commented Sep 8, 2022

okay @Code-Hex! I used a string array variable similar to importTypes to hold enum declarations, which are then concatenated onto the final initialEmit() string. this makes all enum definitions hoisted, preventing any "use before declaration" errors.

I still get a small handful of errors when running npx tsc --noEmit example/zod/schemas.ts, but I believe these are trivial since the only thing I changed about the schema is the placement of the enum declarations. the tsc errors seem to be related to optional field compatibility. let me know if this is an issue / if you see these errors as well.

@Code-Hex
Copy link
Owner

@elijaholmos Thanks!
Did you run npx tsc --noEmit example/myzod/schemas.ts too? There are many errors.
I can't merge it until fixed compilation issues. Sorry.

src/config.ts Outdated
* validationSchemaExportType: const
* ```
*/
validationSchemaExportType?: ValidationSchemaExportType;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add validation for this?
Currently, the config has accepted any values (but are not affected. because some logic treats as function)

@elijaholmos
Copy link
Contributor Author

@Code-Hex pardon the closing of the PR- that was unintentional as I reset my fork branch to match your repo's upstream.

anyways, in the origin/main branch, I run yarn install --frozen-lock, yarn generate, then npx tsc --noEmit example/myzod/schemas.ts, and I see the following output:
image

are you able to confirm this on your end?

@Code-Hex
Copy link
Owner

@elijaholmos Thanks!

Oh, I see. I will fix the problem on my end!

@elijaholmos
Copy link
Contributor Author

@Code-Hex ty! let me know when it is done and I will reopen my PR

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 this pull request may close these issues.

[Feature Request] configurable export type
2 participants