When attempting to apply the IsEnum class validator in the following code:
export class UpdateEvaluationModelForReportChanges {
@IsNotEmpty()
@IsEnum(ReportOperationEnum) // FIRST
operation: ReportOperationEnum;
@IsNotEmpty()
@IsEnum(EvaluationStatusEnum). // SECOND
status: EvaluationStatusEnum;
// ...
}
I got this error in the second IsEnum decorator:
../node_modules/src/decorator/typechecker/IsEnum.ts:18
return Object.entries(entity)
^
TypeError: Cannot convert undefined or null to object
The first enum, ReportOperationEnum
, is defined within the same file as the class. Here's its definition:
// ...
export enum ReportOperationEnum {
COMPLETED = "COMPLETED",
REVIEWED = "REVIEWED",
RETURNED = "RETURNED",
}
// ...
The second enum, EvaluationStatusEnum
, is located in a separate file:
// ...
export enum EvaluationStatusEnum {
CANCELED = 'CANCELED',
SCHEDULED = 'SCHEDULED',
FINISHED = 'FINISHED',
}
// ...
While copying the second enum into the file with the first enum resolves the issue, I prefer not to duplicate or relocate the second enum from its original location. Just in case, both enums are part of files containing additional enums and classes. Are there any potential reasons or related factors that could be causing this problem?
I resolved the issue. It appears to be related to the location of the second enum, EvaluationStatusEnum
, which was in the file exporting the schema:
export enum EvaluationStatusEnum {
// ...
}
@Schema()
export class Evaluation extends BaseModel implements EvaluationModel {
// ...
}
export const EvaluationSchema = SchemaFactory.createForClass(Evaluation);
So, by moving the enum to a third file (which I will now reserve for enums exclusively), the errors disappeared.