Create a zod type from a TS enum
See original GitHub issueRight now if you want to create a type from a TS enum, you have do something like the following:
enum PlantLifecycle {
Evergreen = 'EVERGREEN',
Deciduous = 'DECIDUOUS',
SemiDeciduous = 'SEMI_DECIDUOUS'
};
const lifecycle = z.union([
z.literal(PlantLifecycle.Deciduous),
z.literal(PlantLifecycle.Evergreen),
z.literal(PlantLifecycle.SemiDeciduous),
]);
There should be an easier way of doing this, like:
enum PlantLifecycle {
Evergreen = 'EVERGREEN',
Deciduous = 'DECIDUOUS',
SemiDeciduous = 'SEMI_DECIDUOUS'
};
z.enum(PlantLifecycle);
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Zod: create a schema using an existing type - Stack Overflow
Show activity on this post. If you want to use the type directly you can use this: const methods = ['get','GET',...] as const;...
Read more >colinhacks/zod: TypeScript-first schema validation ... - GitHub
Zod is a TypeScript-first schema declaration and validation library. I'm using the term "schema" to broadly refer to any data type, from a...
Read more >Untitled
nativeEnum ()`, which lets you create z Zod schema from an existing TypeScript ... You can use this to customize certain error-handling behavior:...
Read more >zod - npm
Enums ; Intersections; Tuples; Recursive types. JSON type; Cyclical data ... You can create a Zod schema for any TypeScript primitive.
Read more >zod-to-ts - npm Package Health Analysis - Snyk
generate TypeScript types from your Zod schema For more information about how to use this package see README · Ensure you're using the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@chrbala @maneetgoyal @johncantrell97
hey folks, I implemented this in zod@1.11 as
z.nativeEnum
. documented here. give it a shot!Just implemented a basic version of this that works for both enums and
const
objects.Any alternative naming suggestions? @chrbala @johncantrell97 @maneetgoyal @silasdavis