我为用户提供了几个DTO
首先:
export class UserProfileContactDto {
@ApiProperty()
@IsNotEmpty()
@IsEmail()
readonly email: string;
@ApiProperty()
@IsNotEmpty()
@IsPhoneNumber('ua')
readonly phone: string;
}第二:
export class UserProfileDataDto {
@ApiProperty()
@IsNotEmpty()
@IsString()
readonly name: string;
@ApiProperty({ required: false })
@IsString()
readonly lastname: string;
@ApiProperty()
@IsNotEmpty()
@IsEnum(userGenderEnum)
readonly gender: userGenderEnum;
}我想根据这些DTO获得第三个DTO:
export class UserCreateDto extends UserProfileDataDto, UserProfileContactDto{
@ApiProperty()
@IsNotEmpty()
@IsString()
@Matches(/((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/, {
message: 'password too weak',
})
readonly password: string;
}但是我不能对多个类使用扩展,接口对我也不起作用,因为我使用的是类验证器库。
你怎么能解决我的问题呢?
发布于 2020-10-07 19:51:36
由于TS不支持多重继承,并且您不能使用接口,因此您可以使用联系人数据的组合来解决此问题(尽管它需要您更改接口):
export class UserProfileDataDto {
@ApiProperty()
@IsNotEmpty()
@IsString()
readonly name: string;
@ApiProperty({ required: false })
@IsString()
readonly lastname: string;
@ApiProperty()
@IsNotEmpty()
@IsEnum(userGenderEnum)
readonly gender: userGenderEnum;
@ApiProperty()
@IsNotEmpty()
readonly contactData: UserProfileContactDto;
}然后,您可以从这个类扩展,就像对UserCreateDto所做的那样。
https://stackoverflow.com/questions/64224836
复制相似问题