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

FEAT: callback when he treats a type #88

Open
mathcovax opened this issue May 14, 2024 · 0 comments
Open

FEAT: callback when he treats a type #88

mathcovax opened this issue May 14, 2024 · 0 comments

Comments

@mathcovax
Copy link

mathcovax commented May 14, 2024

I use this library to generate my front typing. But I encounter a problem with certain specific cases.

If my back returns an object with dates like this:

zod.object({
    firstname: zod.string(),
    dateOfBirth: zod.date(),
});

The generated type will be the following :

{
    firstname: string;
    dateOfBirth: Date;
}

A correct typing for the out of the back but wrong for the front, the real type that the front obtains is the following :

{
    firstname: string;
    dateOfBirth: string; // containe Date in this string
}

(Same problem for typing back entries)

Proposed solution :

zodToTs(
    zodSchema, 
    identifier, 
    [
        {
            type: ZodDate,
            callback: (zodDate) => {
                if(zodDate.def.coerce){
                    return zod.string() // replace a current ZodDate meets to ZodString
                }
                return zodDate
            }
        },
    ]
)

The operation would be simple, just before processing a type we look in the list if it has a match and if so launches the callback. the object returned from the callback will be the one that will be processed by the converter.

Thank you for your work. 😄

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