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

TypeError: Cannot read properties of undefined (reading 'left') #9

Closed krisgrm closed 10 months ago

krisgrm commented 1 year ago

i have a rather big but simple zod schema (and i get error below when calling parseZodSchema)

TypeError: Cannot read properties of undefined (reading 'left')
at getKeysFromZodSchema
at 
at Array.reduce
at getKeysFromZodSchema
at getKeysFromZodSchema
at 
at Array.map
at getKeysFromZodSchema
at Proxy.parseZodSchema
krisgrm commented 1 year ago

found the issue problem is with zod enum.. works:

    const schema = z.object({
      a: z.boolean(),
      b: z.string(),
      c: z.string(),
      d: z.string(),
      e: z.string(),
      f: z.string(),
      g: z.string(),
      h: z.array(
        z.object({
          ha: z.number(),
          hb: z.string(),
          hc: z.boolean(),
          hd: z.string(),
        }),
      ),
      i: z.object({
        ia: z.number(),
      }),
    });
    const schemaKeys = parseZodSchema(schema);

Doesn't work

    const schema = z.object({
      a: z.boolean(),
      b: z.string(),
      c: z.enum(["a", "b", "c"]),
      d: z.string(),
      e: z.string(),
      f: z.string(),
      g: z.string(),
      h: z.array(
        z.object({
          ha: z.number(),
          hb: z.string(),
          hc: z.boolean(),
          hd: z.string(),
        }),
      ),
      i: z.object({
        ia: z.number(),
      }),
    });
    const schemaKeys = parseZodSchema(schema);

on another note, my idea / what i am trying to achieve is that I'd like to build a library, with a function that exposes:

zodSchema: union of available zod schemas
field: which-field-in-scheam (this should autocomplete based on zodSchema selected above)
operator: operator-based-on-field-data-type
value: value, which should be type of the selected field

I'd like to achieve all of this with zod, so i can also use zod.parse when I get this data objects from "unreliable".. i could build a type (maybe it would be easier)..

what would you suggest?

raflymln commented 1 year ago

Well, i haven't test Zod Enum yet, i think that's the problem haha 😅

As of now, the supported zod type was as stated below:

ZodUnion, ZodIntersection, ZodArray, ZodOptional, ZodNullable, and ZodObject also of course the basic type like string, boolean and so on

But don't worry i'm planning on supporting the other things such as the Zod enum too

krisgrm commented 1 year ago

Let me know if I can help somehow. I can look into the source code ofcourse but if you have some pointers go ahead.

Also did you see my 2nd part of message, do you have any suggestions?

Feel free to ignore me completely hehe, we are all busy

raflymln commented 1 year ago

Oops sorry missed it, do you mean other than keys and prismaKeys, you wanted other key such as zodSchema, field, operator and value?

I'm currently working on the field one, it'd list all the primitive zod keys inside it, but i just kinda confuse if there is a zod union inside the schema and there's the same key on between union but with different type, what key should i use?

Also for zodSchema do you mean a collection of zod objects from the input model?

And I don't quite understand for the operator and value one, can you tell me more about that?

Thanks!

raflymln commented 1 year ago

Hi @krisgrm, just wanted to let you know, as per v1.5.0 the ZodEnum is supported! and also to make me possible to fulfill your request, can you provide some details as i asked above?