{"record":{"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/2d90846af918af9602e088812d63a035d47cdbe4/packages/zod/src/v4/classic/schemas.ts#L1928-L1964","documentation":"Thrown by ZodEnum.prototype.extract(values) when one of the requested keys is not present in the enum's entries map. extract() builds a new enum containing only the listed keys, so every supplied value must already be an existing enum key.","triggerScenarios":"Calling myEnum.extract(['RED','PURPLE']) when 'PURPLE' was never defined. Passing the enum *values* instead of the enum *keys* to extract(). Renaming a key in the source enum but forgetting to update an extract() call elsewhere.","commonSituations":"Refactoring an enum and leaving stale extract() call sites. Confusing key vs value semantics (in z.enum they coincide for string enums, but native enums with numeric values or renamed keys diverge). Splitting a shared enum into sub-enums at multiple call sites.","solutions":["Ensure every string passed to extract() is a literal key of the original enum; check the enum's .enum or .options first.","If filtering by value rather than key, map values back to keys or use z.enum on the value array directly.","Filter the requested list through Object.keys(enum.entries) before calling extract() so unknown keys are dropped instead of throwing."],"exampleFix":"// before (throws — 'PURPLE' is not a key)\nconst Colors = z.enum(['RED', 'GREEN', 'BLUE']);\nconst Warm = Colors.extract(['RED', 'PURPLE']);\n\n// after\nconst Warm = Colors.extract(['RED', 'BLUE']);","handlingStrategy":"validation","validationCode":"function safeExtract(enumSchema, keys) {\n  const valid = new Set(Object.keys(enumSchema.enum));\n  const unknown = keys.filter((k) => !valid.has(k));\n  if (unknown.length) {\n    throw new Error(`Unknown enum keys: ${unknown.join(', ')}`);\n  }\n  return enumSchema.extract(keys);\n}","typeGuard":"function isEnumKey(enumSchema, key) {\n  return Object.prototype.hasOwnProperty.call(enumSchema.enum, key);\n}","tryCatchPattern":"try {\n  const Sub = MyEnum.extract(requestedKeys);\n} catch (e) {\n  if (e.message.includes('not found in enum')) {\n    // log the offending key and degrade gracefully\n    return null;\n  }\n  throw e;\n}","preventionTips":["Derive extract() argument lists from Object.keys(enum.enum) at the call site so they can never drift.","Add compile-time checks (e.g. `satisfies keyof typeof MyEnum`) for literal arrays.","Unit-test extract() call sites whenever the source enum changes."],"tags":["enum","schema-construction","api-misuse"],"backgroundTag":null,"analyzedSha":"2d90846af918af9602e088812d63a035d47cdbe4","analyzedAt":"2026-08-11T01:21:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-22T11:17:16.035Z"}