Skip to content

TypeError for default values when using enums #640

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
sutt0n opened this issue May 1, 2024 · 7 comments
Closed

TypeError for default values when using enums #640

sutt0n opened this issue May 1, 2024 · 7 comments
Labels
good first issue Good for newcomers

Comments

@sutt0n
Copy link
Contributor

sutt0n commented May 1, 2024

The latest of this wonderful plugin has unfortunately broken default values for us regarding the default stringified values. I'm unsure if others are having this issue as it's very new, and this could very well be an edge-case.

Related PR: #529

The Problem

For example, if one were to have the following enum:

export enum GuyFieriQuotes {
  DinersDriveInsAndDives = "DINERS_DRIVEINS_AND_DIVES",
  Funkalicious = "FUNKALICIOUS",
  WelcomeToFlavortown = "WELCOME_TO_FLAVORTOWN"
}

export const GuyFieriQuotesSchema = z.nativeEnum(GuyFieriQuotes);

We then have the generated schema:

export function FlavortownInputSchema(): z.ZodObject<Properties<FlavortownInput>> {
  return {
    quote: GuyFieriQuotesSchema.default("WELCOME_TO_FLAVORTOWN"),
  };
}

This would throw the following type errors:

Argument of type '"WELCOME_TO_FLAVORTOWN"' is not assignable to parameter of type 'GuyFieriQuotesSchema'.

Output of Solution

Adding this fix/feature generates this output:

export function FlavortownInputSchema(): z.ZodObject<Properties<FlavortownInput>> {
  return {
    quote: GuyFieriQuotesSchema.default(GuyFieriQuotes.WelcomeToFlavortown),
  };
}

I think having a flag to specify enum types' respective paths as their default values as well as a naming convention (since that can be changed) configuration, we should be able to address it.

@sutt0n
Copy link
Contributor Author

sutt0n commented May 1, 2024

I've opened a PR for this issue: #636

@AndKiel
Copy link

AndKiel commented Jun 13, 2024

This is not an edge case. I'm stuck with version 0.12.1 because the default values generation for zod is broken.

@sutt0n
Copy link
Contributor Author

sutt0n commented Jun 13, 2024

@AndKiel Glad to know we're not the only one! 🙂

@DarlonHenrique
Copy link

same thing here

@Code-Hex
Copy link
Owner

Code-Hex commented Jul 7, 2024

I merged the contents of PR #636, will release it as soon as the README is fixed

@Code-Hex
Copy link
Owner

I just published v0.16.0 which is included this fix.

@sutt0n
Copy link
Contributor Author

sutt0n commented Sep 23, 2024

For those on this thread, I noticed that this effort introduced a bug with underscore transformation. Since then, I've opened #828

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants