Loose autocomplete with enums / don't error on unknown values #4939
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
|
Hi @matthew-gladman-oua! I'm Dosu and I’m helping the zod team. Zod doesn't have a built-in "loose enum" feature, but you can get the behavior you want by using a union: import { z } from "zod";
const ExampleSchema = z.object({
example: z.union([z.enum(["hi", "hi2", "hi3"]), z.string()])
});
// TypeScript type:
type Example = z.infer<typeof ExampleSchema>;
// { example: "hi" | "hi2" | "hi3" | (string & {}) }
ExampleSchema.parse({ example: "hi3" }); // passes
ExampleSchema.parse({ example: "something" }); // also passesThis pattern isn't a first-class Zod feature, but it works because of how TypeScript unions and Zod's combinators interact. If you want to see this as a dedicated feature, feel free to tag @colinhacks for further discussion. If this solves your problem, please close the issue! For more on how Zod's To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
|
See #4934. |
Beta Was this translation helpful? Give feedback.


See #4934.