You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除@nestjs/swagger PartialType中DTO属性的默认值?

解决UpdateAdminDto移除jurisdiction默认值的问题

你遇到的问题是因为PartialType仅将原DTO的属性转为可选,但会保留原属性的默认值配置。要移除jurisdiction的默认值,最直接的方式是在UpdateAdminDto中重新声明该属性,覆盖原有的默认值设置:

// UpdateAdminDto.ts
import { PartialType, OmitType } from '@nestjs/swagger';
import { CreateAdminDto } from './create-admin.dto';
import { IsOptional } from 'class-validator';

export class UpdateAdminDto extends PartialType(OmitType(CreateAdminDto, ['username'])) {
  // 重新声明属性,仅保留装饰器和类型,去掉默认值
  @IsOptional()
  readonly jurisdiction?: string | null;
}

这样修改后,当更新接口未传入jurisdiction时,该属性不会携带默认的null值,而是保持undefined或者不出现该字段,符合更新操作的预期——仅修改传入的字段,不覆盖未传入的字段。

内容的提问来源于stack exchange,提问作者Bo.Y

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 06:59:51