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 TS enum and z.nativeEnum openapi zod client #224

Open
palaniichukdmytro opened this issue Sep 7, 2023 · 1 comment
Open

Generate TS enum and z.nativeEnum openapi zod client #224

palaniichukdmytro opened this issue Sep 7, 2023 · 1 comment

Comments

@palaniichukdmytro
Copy link

palaniichukdmytro commented Sep 7, 2023

I think it's common issue for generate type from open api spec.
I am using with zodios https://github.com/astahmer/openapi-zod-client/ openapi-zod-client. But did not found any possible solution to generate enum , instead I see that only union types is possible . Any idea to generate TS enum to use this enum in a code durin compile time .

Example schema json

 {
            "name": "format",
            "in": "query",
            "required": false,
            "schema": {
              "enum": [
                "flat",
                "grouped"
              ],
              "type": "string",
              "default": "flat",
              "title": "Format"
            }

By default it's generate a union with type flag enabled or via handlebars templates you can do yourself. But it is not possible to generate TS enum or z.nativeEnums

Output:

export const Format= z.enum([
  'Flat',
  'Grouped',
])
export type FormatType = z.infer<typeof Format> // union one

Exacted/Desire output

export enum FormatType {
  Flat = 'Flat', 
  Grouped = 'Grouped'
}

export const Format = z.nativeEnum(FormatType)


@palaniichukdmytro palaniichukdmytro changed the title Generate TS enum and union wtih openapi zod client Generate TS enum and z.nativeEnum openapi zod client Sep 7, 2023
@palaniichukdmytro
Copy link
Author

I am thinking it might be helpful to add flag like --export-enums and generate them. Is it refer to this line

return schema.nullable ? t.union([t.string(), t.reference("null")]) : t.string();

smth like this

t.enum(${name}, [
...schema.enum
]);

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

No branches or pull requests

1 participant