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 const array of possible enum values #832

Closed
gwax opened this issue Aug 9, 2024 · 8 comments · Fixed by #901
Closed

Generate const array of possible enum values #832

gwax opened this issue Aug 9, 2024 · 8 comments · Fixed by #901

Comments

@gwax
Copy link
Contributor

gwax commented Aug 9, 2024

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

I would like to have values matching possible enum values that are synchronized to the database schema for looping or use with zod and similar tools.

Describe the solution you'd like
A clear and concise description of what you want to happen.

for a schema:

export type Database = {
  public: {
    Enums: {
      mytype: "value" | "other"
    }
  }
}

I would like to have generated values:

export const DatabaseValues = {
  public: {
    Enums: {
      mytype: ["value", "other"],
    },
  },
} as const;

this would allow defining zod types to match like:

const mySchema = z.object({
  mytype: z.enum(DatabaseValues.public.Enums.mytype),
});

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

  • manually keeping values in sync with the database and hoping they don't fall out of sync
  • writing custom type checkers to ensure that values match the schema

Additional context
Add any other context or screenshots about the feature request here.

@sweatybridge
Copy link
Contributor

Sorry for the late reply on this. I believe we are currently generating typescript enums. Is it not sufficient to zod schema?

@sweatybridge sweatybridge transferred this issue from supabase/cli Nov 15, 2024
@toBeOfUse
Copy link

No, you are generating unions of string literals, which is a different thing. The current types that are generated can only be used for static type-checking (unlike proper Typescript enums.)

@gwax
Copy link
Contributor Author

gwax commented Mar 1, 2025

@sweatybridge , could you please reopen this feature request

As @toBeOfUse notes, the code does not generate typescript enums (which are values), it generates string literal unions (which are types). (export type vs export const)

The types generated currently are removed by the typescript compiler and cannot be used at runtime.

Generated values are useful both for the zod case and for iterating over values.

@toBeOfUse
Copy link

A workaround is present in #864

@soedirgo
Copy link
Member

soedirgo commented Mar 5, 2025

Reopened this gauge interest on generating values in addition to types.

@toBeOfUse
Copy link

This is a kind of a duplicate of #864 now

@toBeOfUse
Copy link

#901 looks good to me

@gwax
Copy link
Contributor Author

gwax commented Mar 14, 2025

@soedirgo , #901 does this

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 a pull request may close this issue.

4 participants