raflymln / zod-key-parser

Parse your Zod schema into keys or Prisma ORM select format
https://www.npmjs.com/package/zod-key-parser
MIT License
16 stars 0 forks source link

Handle union string literals #56

Open PeytonHanel opened 1 month ago

PeytonHanel commented 1 month ago

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 commented 1 month ago

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

PeytonHanel commented 1 month ago

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 commented 1 month ago

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