Zod enums vs Native enums

From what I understood from the documentation of Zod, enum should be use as followed :
const FishEnum = z.enum(["Salmon", "Tuna", "Trout"]);
type FishEnum = z.infer<typeof FishEnum>;

// to use it
const someFish = FishEnum.parse("Salmon");
// to validate
const VALUES = ["Salmon", "Tuna", "Trout"] as const;
const FishEnum = z.enum(VALUES);
const FishEnum = z.enum(["Salmon", "Tuna", "Trout"]);
type FishEnum = z.infer<typeof FishEnum>;

// to use it
const someFish = FishEnum.parse("Salmon");
// to validate
const VALUES = ["Salmon", "Tuna", "Trout"] as const;
const FishEnum = z.enum(VALUES);
But it is also possible to use native enums :
export enum Countries {
Maroc = "Maroc",
Tunisie = "Tunisie",
Egypte = "Egypte",
}

// to use it
const someCountry = Countries.Maroc;
// to validate
const country = z.nativeEnum(Countries);
export enum Countries {
Maroc = "Maroc",
Tunisie = "Tunisie",
Egypte = "Egypte",
}

// to use it
const someCountry = Countries.Maroc;
// to validate
const country = z.nativeEnum(Countries);
Using native enums seems more easy so, why would I use zod enums ? Have I missed something ?
Solution:
This is how I use enums with Zod : ```javascript // Single source of truth: const fishes = ["Salmon", "Tuna", "Trout"] as const...
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
K
KP295d ago
Matt Pocock
YouTube
Enums considered harmful
TypeScript enums are not great. In almost all cases, I use an 'as const' with a sprinkling of type magic to express them. Become a TypeScript Wizard with Matt's TypeScript Course: https://www.totaltypescript.com/ Follow Matt on Twitter https://twitter.com/mattpocockuk
Solution
B
Benjamin295d ago
This is how I use enums with Zod :
// Single source of truth:
const fishes = ["Salmon", "Tuna", "Trout"] as const
// Use in a Zod Schema:
const fishesSchema = z.enum(fishes)
// Type to use in the code:
// = "Salmon" | "Tuna" | "Trout"
type FishType = (typeof fishes)[number];
// Single source of truth:
const fishes = ["Salmon", "Tuna", "Trout"] as const
// Use in a Zod Schema:
const fishesSchema = z.enum(fishes)
// Type to use in the code:
// = "Salmon" | "Tuna" | "Trout"
type FishType = (typeof fishes)[number];
M
Max194d ago
@Benjamin @Romain type: z.nativeEnum(YourPrismaEnum),
Want results from more Discord servers?
Add your server
More Posts
how can modify cookies in route handler with server actionlet say we have page route ```js export defualt function Page(){ const user=await getUser(); if(when is virtualization neccessaryI have an array of 873 objects and im wondering if virtualization is needed, it also needs to be resCSS Naming adviceHey guys, I'm kind of a noob and I was wondering if you could give me some feedback in my css class signout from server in next js 13can anyone tell me how can i signout from server in next auth? signOut() is not working and below coUpdated data not being fetched in production.Hey guys so i just made a web app which uses prisma / planetscale and tanstack query. I have been tSome good old system designHello everyone, how you doing ? So I want to create a little app that consists of 2 main parts sysPages or App ?I'm going to create a production project with nextjs, but confused, What is the best option pages/ oWait for ISR Revalidation (rather than showing stale data)Hey guys! I had a quick question about using ISR in Next.js (app router) with Vercel. As outlined byWhat about Panda CSS?Hey there! I can't find any talk about using Panda CSS over Tailwind on this Discord. I would like tNextAuth sign in function not working properlyhey, i have nextauth set up with `credentialsProvider` on, i have the following onClick on a button:Fetching only onceso i have an authToken being stored in the local storage and i want to send it in a POST request eacimport mysql into planetscaleHas anyone imported a database from your local pc into planetscale? With regard to the hostname what