{"id":"29d0494a8ef5b8e5","repo":"colinhacks/zod","slug":"key-value-not-found-in-enum","errorCode":null,"errorMessage":"Key ${value} not found in enum","messagePattern":"Key (.+?) not found in enum","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v4/classic/schemas.ts","lineNumber":1946,"sourceCode":"    params?: string | core.$ZodEnumParams\n  ): ZodEnum<util.Flatten<Omit<T, U[number]>>>;\n}\nexport const ZodEnum: core.$constructor<ZodEnum> = /*@__PURE__*/ core.$constructor(\"ZodEnum\", (inst, def) => {\n  core.$ZodEnum.init(inst, def);\n  ZodType.init(inst, def);\n  inst._zod.processJSONSchema = (ctx, json, params) => processors.enumProcessor(inst, ctx, json, params);\n\n  inst.enum = def.entries;\n  inst.options = Object.values(def.entries);\n\n  const keys = new Set(Object.keys(def.entries));\n\n  inst.extract = (values, params) => {\n    const newEntries: Record<string, any> = {};\n    for (const value of values) {\n      if (keys.has(value)) {\n        newEntries[value] = def.entries[value];\n      } else throw new Error(`Key ${value} not found in enum`);\n    }\n    return new ZodEnum({\n      ...def,\n      checks: [],\n      ...util.normalizeParams(params),\n      entries: newEntries,\n    }) as any;\n  };\n\n  inst.exclude = (values, params) => {\n    const newEntries: Record<string, any> = { ...def.entries };\n    for (const value of values) {\n      if (keys.has(value)) {\n        delete newEntries[value];\n      } else throw new Error(`Key ${value} not found in enum`);\n    }\n    return new ZodEnum({\n      ...def,","sourceCodeStart":1928,"sourceCodeEnd":1964,"githubUrl":"https://github.com/colinhacks/zod/blob/912f0f51b0ced654d0069741e7160834dca742ee/packages/zod/src/v4/classic/schemas.ts#L1928-L1964","documentation":"Thrown by ZodEnum's `.extract(values)` method, which builds a new enum containing only the listed keys from the original enum's entries (schemas.ts:1941). If any value passed to `.extract()` is not an existing key in `def.entries`, the loop hits the `else` branch and throws. This is a construction-time guard ensuring the subset is valid, not a parse-time validation.","triggerScenarios":"Calling `myEnum.extract([...])` (or `.exclude`) with a key that is not present in `Object.keys(myEnum.enum)`. The check is `keys.has(value)` against the original enum's key set built at schemas.ts:1939.","commonSituations":"Refactoring an enum (renaming or removing a variant) and forgetting to update downstream `.extract()` calls; typos or wrong casing in keys; copy-pasting a key list from another enum; TS inference hiding the mismatch when the enum is typed as a wide Record.","solutions":["Check the key exists first: `Object.keys(myEnum.enum).includes(value)` before calling `.extract()`.","Fix the typo / casing so the value matches an actual key of the enum.","Filter your input list against valid keys: `values.filter((v) => v in myEnum.enum)`.","If the key was legitimately removed, update the call site to drop it from the extract list."],"exampleFix":"// before\nconst Primary = Colors.extract([\"rede\"]); // typo -> throws\n// after\nconst Primary = Colors.extract([\"red\"]);","handlingStrategy":"validation","validationCode":"const keys = Object.keys(myEnum.enum);\nconst want = [\"red\", \"green\"];\nconst missing = want.filter((k) => !keys.includes(k));\nif (missing.length) throw new Error(`Unknown enum keys: ${missing.join(\", \")}`);\nconst Primary = myEnum.extract(want);","typeGuard":"// ZodEnum values are strings; guard the key list before extract.\nfunction areEnumKeys<T extends Record<string, string>>(\n  e: T,\n  vals: readonly string[]\n): vals is (keyof T)[] {\n  return vals.every((v) => v in e);\n}\nif (areEnumKeys(myEnum.enum, want)) myEnum.extract(want);","tryCatchPattern":null,"preventionTips":["Treat enum key lists as derived from `Object.keys(myEnum.enum)`, not hand-typed.","Run a unit test that every key passed to `.extract()`/`.exclude()` exists in the source enum.","When refactoring an enum, grep for `.extract(` and `.exclude(` usages of that enum."],"tags":["enum","refactoring","typo","construction-time"],"analyzedSha":"912f0f51b0ced654d0069741e7160834dca742ee","analyzedAt":"2026-08-03T17:41:55.908Z","schemaVersion":2}