{"id":"cec93a71210c7ac3","repo":"sindresorhus/is","slug":"expected-value-which-is-enumcase-received-value","errorCode":null,"errorMessage":"Expected value which is `EnumCase`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `EnumCase`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1592,"sourceCode":"\t\tthrow new TypeError(message ?? typeErrorMessage('empty set', value));\n\t}\n}\n\nexport function assertEmptyString(value: unknown, message?: string): asserts value is '' {\n\tif (!isEmptyString(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('empty string', value));\n\t}\n}\n\nexport function assertEmptyStringOrWhitespace(value: unknown, message?: string): asserts value is '' | Whitespace {\n\tif (!isEmptyStringOrWhitespace(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('empty string or whitespace', value));\n\t}\n}\n\nexport function assertEnumCase<T = unknown>(value: unknown, targetEnum: T, message?: string): asserts value is T[keyof T] {\n\tif (!isEnumCase(value, targetEnum)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('EnumCase', value));\n\t}\n}\n\nexport function assertError(value: unknown, message?: string): asserts value is Error {\n\tif (!isError(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Error', value));\n\t}\n}\n\nexport function assertEvenInteger(value: number, message?: string): asserts value is number {\n\tif (!isEvenInteger(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('even integer', value));\n\t}\n}\n\nexport function assertFalsy(value: unknown, message?: string): asserts value is Falsy {\n\tif (!isFalsy(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('falsy', value));","sourceCodeStart":1574,"sourceCodeEnd":1610,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1574-L1610","documentation":"Thrown by `assertEnumCase` (source/index.ts:1590) when the value is not one of the values of the supplied enum object. `isEnumCase` checks membership against `Object.values(targetEnum)`, so the assertion validates that an untrusted value maps to an actual enum case before it's used as `T[keyof T]`.","triggerScenarios":"Calling `assert.enumCase(value, MyEnum)` where `value` is not among `Object.values(MyEnum)` — e.g. passing an enum key name instead of its value, a raw string with wrong casing, or a stale value after the enum was changed.","commonSituations":"Parsing enum values from query strings, JSON payloads, or environment variables; numeric TypeScript enums where reverse-mapping keys pollute `Object.values` expectations; an enum member being renamed or removed in a version upgrade while persisted data still holds the old value.","solutions":["Verify the raw value matches an enum VALUE, not a key — for `enum Color {Red = 'red'}` pass `'red'`, not `'Red'`","Check for casing/format mismatches between the external source (API, env var) and the enum definition","After enum changes, migrate persisted/serialized data that still contains removed values","Use `is.enumCase(value, MyEnum)` to branch and supply a default instead of throwing"],"exampleFix":"// before\nassert.enumCase('Red', Color); // key name, throws\n// after\nassert.enumCase('red', Color); // matches Color.Red = 'red'","handlingStrategy":"validation","validationCode":"const ALLOWED = Object.values(MyEnum);\nif (ALLOWED.includes(value)) {\n  assert.enumCase(value, MyEnum);\n}","typeGuard":"function isEnumCase<T extends Record<string, unknown>>(value: unknown, targetEnum: T): value is T[keyof T] {\n  return Object.values(targetEnum).includes(value);\n}","tryCatchPattern":"try {\n  assert.enumCase(value, MyEnum);\n} catch (error) {\n  if (error instanceof TypeError) {\n    // value is not one of the enum's values — often stale data or an unmapped API string\n  } else throw error;\n}","preventionTips":["Validate external/API strings against Object.values(Enum) at the trust boundary before asserting","Remember numeric TypeScript enums have reverse mappings — Object.values includes both keys and numbers, which can cause surprises","Centralize enum parsing in one function so new cases are handled in a single place"],"tags":["type-assertion","runtime-validation","enum","typescript"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}