Validating Enum Types with Zod Guide
Q: Can you explain how to validate an enum type using Zod?
- Zod
- Mid level question
Explore all the latest Zod interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Zod interview for FREE!
Certainly! Zod is a TypeScript-first schema declaration and validation library that allows for the creation of complex validation schemas with ease. To validate an enum type using Zod, you can utilize the `z.enum()` method, which lets you define a Zod schema for string literal enums.
Here’s how you can create and validate an enum using Zod:
1. Define the Enum: First, you need to define a TypeScript enum or a union of string literals.
2. Create the Zod Schema: Use `z.enum()` to create a validation schema based on the defined enum.
### Example with TypeScript Enum
```typescript
enum Role {
Admin = "ADMIN",
User = "USER",
Guest = "GUEST",
}
// Creating the Zod schema
const RoleSchema = z.enum([Role.Admin, Role.User, Role.Guest]);
// Validating a value
const result = RoleSchema.safeParse("ADMIN");
if (result.success) {
console.log("Valid role:", result.data); // Output: Valid role: ADMIN
} else {
console.error("Invalid role:", result.error);
}
```
### Example with String Literal Union
If you don’t need a TypeScript enum, you can directly use a union of string literals:
```typescript
const RoleSchema = z.enum(["ADMIN", "USER", "GUEST"]);
// Validating a value
const result = RoleSchema.safeParse("USER");
if (result.success) {
console.log("Valid role:", result.data); // Output: Valid role: USER
} else {
console.error("Invalid role:", result.error);
}
```
### Clarification
The `z.enum()` method will ensure that any value validated against the schema is one of the specified enum values. If a value outside of the defined enum options is provided, Zod will produce an error indicating the validation failure, which helps maintain the integrity of the expected values throughout your application.
Here’s how you can create and validate an enum using Zod:
1. Define the Enum: First, you need to define a TypeScript enum or a union of string literals.
2. Create the Zod Schema: Use `z.enum()` to create a validation schema based on the defined enum.
### Example with TypeScript Enum
```typescript
enum Role {
Admin = "ADMIN",
User = "USER",
Guest = "GUEST",
}
// Creating the Zod schema
const RoleSchema = z.enum([Role.Admin, Role.User, Role.Guest]);
// Validating a value
const result = RoleSchema.safeParse("ADMIN");
if (result.success) {
console.log("Valid role:", result.data); // Output: Valid role: ADMIN
} else {
console.error("Invalid role:", result.error);
}
```
### Example with String Literal Union
If you don’t need a TypeScript enum, you can directly use a union of string literals:
```typescript
const RoleSchema = z.enum(["ADMIN", "USER", "GUEST"]);
// Validating a value
const result = RoleSchema.safeParse("USER");
if (result.success) {
console.log("Valid role:", result.data); // Output: Valid role: USER
} else {
console.error("Invalid role:", result.error);
}
```
### Clarification
The `z.enum()` method will ensure that any value validated against the schema is one of the specified enum values. If a value outside of the defined enum options is provided, Zod will produce an error indicating the validation failure, which helps maintain the integrity of the expected values throughout your application.


