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

Support circular dependencies with loops of length > 1 #114

Merged
merged 2 commits into from Mar 13, 2023

Conversation

NexZhu
Copy link
Contributor

@NexZhu NexZhu commented Feb 1, 2023

Why

This PR add support for any circular dependencies with loops of length > 1 (unless there are missing dependencies)

Before this PR, This repo already supported types depending on itself with z.lazy() like:

interface Person {
  friend: Person
}

With z.lazy() actually circular dependencies with loops of length > 1 can also be supported, for example:

      export interface EvilPlan {
        description: string;
        details: EvilPlanDetails;
      }

      export interface EvilPlanDetails {
        parent: EvilPlan;
        steps: string[];
      }

With this PR will generate:

        export const evilPlanSchema: z.ZodSchema<EvilPlan> = z.lazy(() => z.object({
            description: z.string(),
            details: evilPlanDetailsSchema
        }));

        export const evilPlanDetailsSchema: z.ZodSchema<EvilPlanDetails> = z.lazy(() => z.object({
            parent: evilPlanSchema,
            steps: z.array(z.string())
        }));

@codecov-commenter
Copy link

Codecov Report

Merging #114 (865b912) into main (e5e4961) will decrease coverage by 2.95%.
The diff coverage is 93.46%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##             main     #114      +/-   ##
==========================================
- Coverage   98.49%   95.55%   -2.95%     
==========================================
  Files           8       13       +5     
  Lines         332      517     +185     
  Branches      102      172      +70     
==========================================
+ Hits          327      494     +167     
- Misses          5       20      +15     
- Partials        0        3       +3     
Impacted Files Coverage Δ
src/utils/traverseTypes.ts 68.96% <68.96%> (ø)
src/core/generateZodSchema.ts 95.19% <93.02%> (-2.02%) ⬇️
src/utils/resolveModules.ts 95.91% <95.91%> (ø)
src/core/generate.ts 100.00% <100.00%> (ø)
src/core/jsDocTags.ts 98.57% <100.00%> (-1.43%) ⬇️
src/core/validateGeneratedTypes.ts 97.05% <100.00%> (-0.31%) ⬇️
src/utils/getSimplifiedJsDocTags.ts 100.00% <100.00%> (ø)
src/utils/isNotNull.ts 100.00% <100.00%> (ø)
src/utils/resolveDefaultProperties.ts 100.00% <100.00%> (ø)
... and 7 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@NexZhu
Copy link
Contributor Author

NexZhu commented Feb 1, 2023

This PR addionally improve the generation process:

  1. Every progressive iteration will at least find a direct or indirect missing dependency (1st iteration will find all direct missing dependencies, 2nd iteration will find all indirect missing dependencies depending on any direct missing dependencies, etc.), or at least one new type that can be generated (all its dependencies become able to be generated by this time).
  2. If an iteration did not find any direct or indirect missing dependencies nor any new types that can be generated, the whole process can immediately end since no further iterations can possibly find them again.
  3. To sum up, we can use a done flag to track if each iteration is still making progress and end the process as soon as there's no more progress being made, this way there won't be wasted iterations and dead loops won't happen.
  4. Max iteration number flag is removed since it's not needed any more.

@NexZhu
Copy link
Contributor Author

NexZhu commented Feb 1, 2023

@fabien0102 Please check this out soon when you have time, so I can open further improvement PRs after this get merged, thanks! 😄

Copy link
Owner

@fabien0102 fabien0102 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just amazing! Huge thanks for this amazing contribution!!! 😍

@fabien0102 fabien0102 merged commit b0eb555 into fabien0102:main Mar 13, 2023
@github-actions github-actions bot mentioned this pull request Mar 13, 2023
@NexZhu
Copy link
Contributor Author

NexZhu commented Mar 15, 2023

@fabien0102 Thank you too for this amazing tool!

fracalo added a commit to fracalo/ts-to-zod that referenced this pull request Apr 7, 2023
* upstream/main:
  fix: support numeric literal keys (fabien0102#120)
  chore: release 3.0.0 (fabien0102#119)
  feat: Generate inferred types (fabien0102#85)
  feat!: Support circular dependencies with loops of length > 1 (fabien0102#114)
  build: add missing dependency on @oclif/errors (fabien0102#115)
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 this pull request may close these issues.

None yet

3 participants