Skip to content
Sarj Code Standards

no-zod-native-enum

Disallow z.nativeEnum() (and z.enum() over a TypeScript enum); use z.enum(["a", "b"]) with a string-literal union instead.

Why

The project prefers literal-first schema definitions. nativeEnum also accepts plain enum-like objects, so this is an explicit declaration policy, not proof that every call duplicates a TypeScript enum's runtime object.

Fix

Pass string literals directly to z.enum and derive the TypeScript type with z.infer.

Examples

Before — flagged Do not hide an imported TypeScript enum behind z.enum
src/status-values.ts
export enum Status {
Active = "active",
Inactive = "inactive",
}
src/status.ts
import { z } from "zod";
import { Status } from "./status-values.js";
export const StatusSchema = z.enum(Status);
After — preferred Let the schema own its literal value domain
src/status.ts
import { z } from "zod";
export const StatusSchema = z.enum(["active", "inactive"]);
Before — flagged Declare literal values instead of an enum-like object
src/status.ts
import { z } from "zod";
const S = z.nativeEnum({ Active: "active", Inactive: "inactive" });
After — preferred Declare string values directly in Zod
src/status.ts
import { z } from "zod";
const S = z.enum(["active", "inactive"]);