javascript - 我如何删除 @nestjs/swagger PartialType 函数中由 dto 属性定义的默认值?
CreateAdminDto.ts
export class CreateAdminDto {
readonly username!: string;
@IsOptional()
readonly jurisdiction?: string | null = null;
@IsOptional()
readonly nickname?: string;
}
UpdateAdminDto.tsexport class UpdateAdminDto extends PartialType(OmitType(CreateAdminDto, ['username'])) {}
在 CreateAdminDto 中,我定义了一个 jurisdiction 属性并设置了默认值。在 UpdateAdminDto 中,我使用了 @nestjs/swagger 的 PartialType 函数,当我进行更新时,我没有传递 jurisdiction 属性,但它仍然有默认值。如何移除这个默认值?
答案#1
PartialType 只是一个类型定义,它不会在运行时剥离值。在运行时,当 UpdateAdminDto 被实例化时,CreateAdminDto 也会被实例化,因此 firstName 的默认值是 admin。使用不同的 DTO。此外,除非它是一个非常基础的 CRUD 应用,否则用于创建的 DTO 和用于更新的 DTO 很可能不会匹配。
https://discord.com/channels/520622812742811698/1276465626470158367/1276608711107809433
可能是因为更新的 Dto 使用了
createDto导出的类 UpdateAdminDto extends PartialType(CreateAdminDto) {} 基本上意味着采用CreateAdminDto并将所有字段设为可选,然后将其应用到UpdateAdminDto
https://discord.com/channels/520622812742811698/1276465626470158367/1276608414109270068