register-user.dto.ts:
export default class RegisterUserDTO {
@IsNotEmpty()
@IsString()
firstName: string;
@IsNotEmpty()
@IsString()
lastName: string;
@IsNotEmpty()
@IsString()
@IsEmail()
email: string;
}
student-register.dto.ts:
export default class StudentRegisterDTO extends RegisterUserDTO {
@IsISO8601()
@IsOptional()
dateOfBirth?: string;
@IsString()
@IsOptional()
address?: string;
@IsNotEmpty()
@IsEnum(UserRoles)
role: UserRoles.STUDENT;
}
trainer-register.dto.ts:
export default class TrainerRegisterDto extends RegisterUserDTO {
@IsNotEmpty()
@IsString()
specialization: string;
@IsNotEmpty()
@IsEnum(UserRoles)
role: UserRoles.TRAINER;
}
nestjs controller:
@Post("register")
async register(
@Body() studentOrTrainerDTO: StudentRegisterDTO | TrainerRegisterDTO,
) {
return await this.authService.register(studentOrTrainerDTO);
}
So whenever I test this endpoint with Postman, class-validator validates nothing on wrong input, however if I test this on only one class type (either StudentRegisterDTO or TrainerRegisterDTO), class-validator works perfectly fine, which made me think it just won't work on union types, why is that and is there a solution to this?