colinhacks / tozod

MIT License
153 stars 7 forks source link

Support for `Enum` Values #6

Open xTVaser opened 3 years ago

xTVaser commented 3 years ago

Enum values don't seem to be supported currently, a minimal example:

type TestType = {
  enumVar: "val1" | "val2";
};

export const Test: toZod<TestType> = z.object({
  enumVar: z.enum(["val1", "val2"]),
});

Produces the following:

Type 'ZodObject<{ enumVar: ZodEnum<["val1", "val2"]>; }, "passthrough", ZodTypeAny, { enumVar: "val1" | "val2"; }, { enumVar: "val1" | "val2"; }>' is not assignable to type 'ZodObject<{ enumVar: never; }, "passthrough", ZodTypeAny, TestType, TestType>'.
  Type '{ enumVar: z.ZodEnum<["val1", "val2"]>; }' is not assignable to type '{ enumVar: never; }'.
    Types of property 'enumVar' are incompatible.
      Type 'ZodEnum<["val1", "val2"]>' is not assignable to type 'never'.

I suspect this may be related to #4 as under the hood, ZodEnums are just ZodUnions.

lzehrung commented 1 year ago

Here's a PR with a version that supports enums as well: https://github.com/colinhacks/tozod/pull/27

inlike-github commented 9 months ago

Do we have an update on this? I am trying to use toZod with a type that has an enum prop. After toZod enum is never.