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

Add option to create string enums based on JSON enums #262

Closed
wants to merge 3 commits into from

Conversation

cdietschrun
Copy link

Addresses #200 . Tests seem to do what I expect and the snapshot file generates what I want as well.

I'm hopeful this can be merged and released soon, or at least reviewed and I can reply to feedback ASAP.

Thanks!

@cdietschrun
Copy link
Author

bump? Otherwise I may end up using a custom version of this package in our flows here internally at Microsoft until this gets reviewed/released

@cdietschrun
Copy link
Author

I published and installed a dev version of this locally here and see

error TypeError: Cannot read property 'endsWith' of undefined
    at C:\Users\chdietsc\source\repos\mason-ui\node_modules\json-schema-to-typescript\dist\src\generator.js:144:25
    at generateRawType (C:\Users\chdietsc\source\repos\mason-ui\node_modules\json-schema-to-typescript\dist\src\generator.js:145:11)
    at generateType (C:\Users\chdietsc\source\repos\mason-ui\node_modules\json-schema-to-typescript\dist\src\generator.js:129:16)
    at C:\Users\chdietsc\source\repos\mason-ui\node_modules\json-schema-to-typescript\dist\src\generator.js:241:47
    at Array.map (<anonymous>)
    at generateInterface (C:\Users\chdietsc\source\repos\mason-ui\node_modules\json-schema-to-typescript\dist\src\generator.js:239:14)
    at generateStandaloneInterface (C:\Users\chdietsc\source\repos\mason-ui\node_modules\json-schema-to-typescript\dist\src\generator.js:278:11)
    at declareNamedInterfaces (C:\Users\chdietsc\source\repos\mason-ui\node_modules\json-schema-to-typescript\dist\src\generator.js:70:128)
    at C:\Users\chdietsc\source\repos\mason-ui\node_modules\json-schema-to-typescript\dist\src\generator.js:79:57
    at Array.map (<anonymous>)

which I'm trying to debug now, but am not an expert with npm/Node/console logging in this case (not seeing debugging messages I'd expect to see)

@cdietschrun
Copy link
Author

cdietschrun commented Oct 17, 2019

Looks like that error was an interesting edge case and when removed from my schemas it passes. It's very likely that's a bad edge case/incorrect schema definition as it was old and barely used for us.

I'd go back and say that this PR is ready for review/merging when possible.

Sigh, nevermind. I guess I don't fully understand how this all gets generated together, as in my test case, my enums aren't being placed anywhere, only referenced by enum name in my output TS file, but they are there without my new flag. This is unfortunate for me as I'm nearing this great tool because I can't manage to make it work for what I need to do with it.

Copy link
Owner

@bcherny bcherny left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution, and sorry about the delay in taking a look at this!

What do you think about taking a slightly different approach here?

  1. Instead of enableStringEnums, would you mind calling the flag inferStringEnumKeysFromValues?
  2. Instead of implementing it as logic in the parser, could we implement it as a normalizer step that adds tsEnumNames to an enum if inferStringEnumKeysFromValues is set (and skips the enum if tsEnumNames is already defined on it)? You'll need to update the normalize function to also pass options to each normalizer step.

@RXminuS
Copy link

RXminuS commented Feb 14, 2020

@cdietschrun thank you so much for adding this! Do you need any help with the requested changes by @bcherny?

@nabati
Copy link

nabati commented Apr 26, 2021

I can probably pick this up, if you don't mind @bcherny

@erik-am
Copy link

erik-am commented Aug 16, 2021

I would love if this feature made it in!

@ghost
Copy link

ghost commented Aug 17, 2021

@cdietschrun @bcherny I'm going to pick this up over the weekend if there are no objections

@overbit
Copy link

overbit commented Nov 28, 2022

Any update on this PR? Worthless to mention the benefits of this

@bcherny
Copy link
Owner

bcherny commented May 4, 2023

Closing out stale PRs. Feel free to rebase and re-open if you like.

@bcherny bcherny closed this May 4, 2023
viplmad added a commit to viplmad/json-schema-to-typescript that referenced this pull request Oct 18, 2023
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.

6 participants