Skip to content

unacy / validateEnum

Function: validateEnum() ​

ts
function validateEnum(value): value is EnumType;

Defined in: packages/core/src/utils/validation.ts:102

Validate that a runtime value is a valid TypeScript enum object.

Accepts numeric enums (with reverse-mapped keys filtered out) and string enums. Rejects empty objects and mixed enums whose forward entries contain both string and number values.

Parameters ​

ParameterTypeDescription
valueunknownThe value to validate

Returns ​

value is EnumType

true if value is a valid EnumType

Throws ​

If the enum contains both numeric and string members

Example ​

typescript
enum LogLevel { DEBUG = 0, INFO = 1 }
validateEnum(LogLevel); // true
validateEnum({}); // false

Use When ​

You are receiving a runtime value and need to confirm it is a valid TypeScript enum before using it as a TypedMetadata<EnumType>.type field.

Pitfalls ​

NEVER use validateEnum to check plain objects that happen to have string values — they may look like string enums but carry no semantic meaning. Use validateRecordSchema for plain data objects.

Released under the MIT License.