There is a field named postData in EmailTypeDto, but it has different types based on conditions. It may be confusing to explain in words, but the code makes it clear.
export class EmailTypeDto {
@IsEnum(EmailType)
public type: EmailType;
@ValidateIf((o) => o.type === EmailType.ACCOUNT_SUCCESS)
@Type(() => AccountSuccessDto)
@ValidateNested()
postData: AccountSuccessDto;
@ValidateIf((o) => o.type === EmailType.MAIN_INQUIRY)
@Type(() => MainInquiryDto)
@ValidateNested()
postData: MainInquiryDto;
}
An error occurs stating that postData cannot be duplicated. Although the postData field name remains constant, its value differs based on the email type, with these variations determined within each email type DTO.
I attempted to solve this issue using @ValidatorConstraint without success. The approach was similar to this proposed solution, but did not work as expected.
@ValidatorConstraint({ name: 'EmailTypeValidation', async: true })
export class EmailTypeValidation implements ValidatorConstraintInterface {
async validate(postData: object, args: ValidationArguments) {
switch (JSON.parse(JSON.stringify(args.object)).type) {
case EmailType.TEST:
postData = AccountSuccessDto;
return true;
case EmailType.MAIN_INQUIRY:
postData = MainInquiryDto;
return true;
}
}
}
I experimented with different parameters for @Type and @ValidateIf, but encountered the same issue.