question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create a zod type from a TS enum

See original GitHub issue

Right 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:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
colinhackscommented, Aug 30, 2020

@chrbala @maneetgoyal @johncantrell97

hey folks, I implemented this in zod@1.11 as z.nativeEnum. documented here. give it a shot!

6reactions
colinhackscommented, Aug 5, 2020

Just implemented a basic version of this that works for both enums and const objects.

const Fruits = { Apple: 'apple', Banana: 'banana' } as const;
const fruitEnum = z.fromEnum(Fruits);

// "apple" | "banana"
type fruitEnum = z.infer<typeof fruitEnum>;
enum Fruits {
  Apple = 'apple',
  Banana = 'banana',
}
const fruitEnum = z.fromEnum(Fruits);

// Fruits
type fruitEnum = z.infer<typeof fruitEnum>;

Any alternative naming suggestions? @chrbala @johncantrell97 @maneetgoyal @silasdavis

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found