Skip to content

TypeError for default values when using enums #640

Closed
@sutt0n

Description

@sutt0n

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions