首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何构建基于多个DTO的DTO?

如何构建基于多个DTO的DTO?
EN

Stack Overflow用户
提问于 2020-10-06 19:26:55
回答 1查看 137关注 0票数 0

我为用户提供了几个DTO

首先:

代码语言:javascript
复制
export class UserProfileContactDto {
  @ApiProperty()
  @IsNotEmpty()
  @IsEmail()
  readonly email: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsPhoneNumber('ua')
  readonly phone: string;
}

第二:

代码语言:javascript
复制
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:

代码语言:javascript
复制
export class UserCreateDto extends UserProfileDataDto, UserProfileContactDto{  

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  @Matches(/((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/, {
    message: 'password too weak',
  })
  readonly password: string;
}

但是我不能对多个类使用扩展,接口对我也不起作用,因为我使用的是类验证器库。

你怎么能解决我的问题呢?

EN

回答 1

Stack Overflow用户

发布于 2020-10-07 19:51:36

由于TS不支持多重继承,并且您不能使用接口,因此您可以使用联系人数据的组合来解决此问题(尽管它需要您更改接口):

代码语言:javascript
复制
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所做的那样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64224836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档