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

Inheritance and reference type search for name filtering #104

Merged
merged 4 commits into from Jan 26, 2023

Conversation

Mayandev
Copy link
Contributor

@Mayandev Mayandev commented Dec 26, 2022

Why

Name filtering could be more intelligent. With this PR, all inheritance and properties reference type can be generated automatically.

Before:

export type Name = "superman" | "clark kent" | "kal-l";
export type Base = {
    sex: 'male' | 'female'
}
export interface Superman extends Base {
  name: Name;
}

// generate nothing
const { getZodSchemasFile } = generate({
  sourceText,
  nameFilter: (id) => id === "Superman"
});

After:

export type Name = "superman" | "clark kent" | "kal-l";
export type Base = {
    sex: 'male' | 'female'
}
export interface Superman extends Base {
  name: Name;
}

const { getZodSchemasFile } = generate({
  sourceText,
  nameFilter: (id) => id === "Superman"
});

// generate schema because of the type reference searching
import { z } from "zod";

export const nameSchema = z.union([
  z.literal("superman"),
  z.literal("clark kent"),
  z.literal("kal-l"),
]);

export const baseSchema = z.object({
  sex: z.union([z.literal("male"), z.literal("female")]),
});

export const supermanSchema = baseSchema.extend({
  name: nameSchema,
});

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.

Sorry for the review delay! Amazing job, I love the feature, just a few things to make your PR even better 😁

Thanks a lot for your contribution!

package.json Outdated Show resolved Hide resolved
src/core/generate.ts Outdated Show resolved Hide resolved
src/core/generate.ts Outdated Show resolved Hide resolved
src/utils/traverseTypes.ts Outdated Show resolved Hide resolved
src/core/generate.ts Outdated Show resolved Hide resolved
Co-authored-by: Fabien BERNARD <fabien0102@hotmail.com>
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.

Great job!

@fabien0102 fabien0102 merged commit 038b9f6 into fabien0102:main Jan 26, 2023
@github-actions github-actions bot mentioned this pull request Jan 26, 2023
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

2 participants