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

Handle union string literals #56

Open
PeytonHanel opened this issue Sep 11, 2024 · 3 comments
Open

Handle union string literals #56

PeytonHanel opened this issue Sep 11, 2024 · 3 comments

Comments

@PeytonHanel
Copy link

Would it be possible to access string literals somehow? I want to define a property in my validation object which only accept an OR union of string literals.

export const schema = parseZodSchema(z.object({
  size: z.union([
    z.literal('S'),
    z.literal('M'),
    z.literal('L'),
    z.literal('XL'),
  ]),
})

// this is equivalent to this type:
type Size = 'S' | 'M' | 'L' | 'XL'

I'm hoping to have something like this:

schema.keys.size.S // => "S"

But I'm not sure if this possible with how Typescript works.

@raflymln
Copy link
Owner

Interesting, let me try this one soon! it seems it could've work

@PeytonHanel
Copy link
Author

PeytonHanel commented Sep 17, 2024

Thank you, I would really appreciate this feature!

Another consideration could be creating a new property on schema next to keys, could be called options or literals or something. It could be the place where all literal values and other values could go and could look like this:

schema.keys.size //  => 'size'
schema.options.size.S // => 'S'

and possibly even a way to get all of them

schema.options.size.all // => ['S', 'M', 'L']

Or the object could just be converted to an array

@PeytonHanel
Copy link
Author

Any updates to this? This would really help my project out.

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

2 participants