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

Creates invalid types and lazy #132

Open
drexxdk opened this issue May 2, 2023 · 1 comment · May be fixed by #133
Open

Creates invalid types and lazy #132

drexxdk opened this issue May 2, 2023 · 1 comment · May be fixed by #133

Comments

@drexxdk
Copy link

drexxdk commented May 2, 2023

Im using https://activitybff.dev.alineadigital.dk/swagger/v1/swagger.json

pnpx openapi-zod-client 'https://activitybff.dev.alineadigital.dk/swagger/v1/swagger.json' -o './src/schemas/activitybff.ts' --base-url https://activitybff.dev.alineadigital.dk --export-schemas

It creates

const AccordionItemDTO: z.ZodType<AccordionItemDTO> = z.lazy(() =>
  z.object({
    heading: z.string(),
    sections: z.array(
      z.union([
        AudioAssetSectionDTO,
        ContentSectionDTO,
        DocumentSectionDTO,
        GeogebraAssetSectionDTO,
        HeadlineSectionDTO,
        ImageAssetSectionDTO,
        LinkSectionDTO,
        QuoteSectionDTO,
        SlideshowSectionDTO,
        TableAssetSectionDTO,
        VideoAssetSectionDTO,
        AssignmentSectionDTO,
        BoxSectionDTO,
        AccordionSectionDTO,
      ])
    ),
    defaultOpen: z.boolean(),
  })
);

the type:

type AccordionItemDTO = {
  heading: string;
  sections: Array<
    | 'AudioAssetSectionDTO'
    | 'ContentSectionDTO'
    | 'DocumentSectionDTO'
    | 'GeogebraAssetSectionDTO'
    | 'HeadlineSectionDTO'
    | 'ImageAssetSectionDTO'
    | 'LinkSectionDTO'
    | 'QuoteSectionDTO'
    | 'SlideshowSectionDTO'
    | 'TableAssetSectionDTO'
    | 'VideoAssetSectionDTO'
    | 'AssignmentSectionDTO'
    | 'BoxSectionDTO'
    | 'AccordionSectionDTO'
  >;
  defaultOpen: boolean;
};

How am i supposed to use that? The two dont match and its using z.lazy that means it thinks it can be undefined which it cant.

Typescript thinks sections are array of strings with specific names. That is wrong. Its array of different objects.

Is there a trick to get it to be accurate?

astahmer added a commit that referenced this issue May 2, 2023
@astahmer astahmer linked a pull request May 2, 2023 that will close this issue
@astahmer
Copy link
Owner

astahmer commented May 2, 2023

Screenshot 2023-05-02 at 15 26 39

I think I fixed the 1st issue (TS type reference sometimes would output strings) that you described, there still seems to be a problem with .lazy in your case, I haven't dug deeper, feel free to do so from here

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.

2 participants