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

Improvements for using discriminatedUnion #270

Open
Ilrilan opened this issue Sep 19, 2024 · 6 comments
Open

Improvements for using discriminatedUnion #270

Ilrilan opened this issue Sep 19, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@Ilrilan
Copy link

Ilrilan commented Sep 19, 2024

Feature description

Sometimes we have a union types with discriminated union, Zod can parse this case with help of discriminator, boosting performance. Can i modify configuration of ts-to-zod for customizing result schema now, or this feature need some enchancements of tool code?

Input

// typescript type or interface causing the output
type MyApiResult = { type:'A', foo: string } | { type:'B', bar: integer } | { type: 'C', baz: boolean }

Output

z.discriminatedUnion("type", [
  z.object({ type: z.literal("A"), foo: z.string() }),
  z.object({ type: z.literal("B"), bar: z.integer() }),,
  z.object({ type: z.literal("C"), bar: z.boolean() }),
])
@tvillaren tvillaren added the enhancement New feature or request label Oct 7, 2024
@tvillaren
Copy link
Collaborator

Hey @Ilrilan,

This is not implemented in the lib yet.

We would need to give a hint to ts-to-zod as to which property should be used as the discriminator. For instance, relying on the JSDocTag syntax we use:

/**
 * @discriminator type
 */
type MyApiResult = { type:'A', foo: string } | { type:'B', bar: number } | { type: 'C', baz: boolean }

Would that make sense?

@Ilrilan
Copy link
Author

Ilrilan commented Oct 10, 2024

Yes, this variant will be good solution.

@Artemnsk
Copy link

Hey! May such features be available via both JSDoc and some cli option? As an alternative way to pass these hints. This cli option may be just a path to a config file with such hints in some format (as it may appear to be messy to pass such a big instructions "as is" via cli).

I'm asking about it because I have already auto-generated (swagger) ts file and I would prefer to not touch the file itself before applying ts-to-zod to it. Sure I can make such changes to my file "on fly" and pipe to ts-to-zod, but it may be not that user-friendly. 🙂 Thanks in advance!

@tvillaren
Copy link
Collaborator

Hello @Artemnsk,

What would be your suggestion for the config part?
We would need to have some kind of new mapping somewhere for each type we want to handle as a discriminated union, something like:

export type Config = {

  //...

  /**
   * A record of discriminated unions with their corresponding type names.
   */
  discriminatedUnions?: Record<string, string>;
};

with instantiation as

{
  MyApiResult: "type"
}

for instance?

That could work, but only for named union types.

@yuriibut
Copy link

yuriibut commented Nov 7, 2024

cool, can't wait 😛

@tvillaren
Copy link
Collaborator

The @discriminator tag has been implemented and is available in 3.14
The config part is not implemented yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants