I have a DTO to create a product
export class CreateProductDto {
@IsString()
title!: string
@IsString()
description!: string
@IsString()
url!: string
@IsNumber()
@Min(0)
price!: number
@IsArray()
@ValidateNested({ each: true })
@ArrayMinSize(1)
@Type(() => ImageDto)
images!: ImageDto[]
@IsArray()
@ValidateNested({ each: true })
@ArrayMinSize(1)
@Type(() => ProductSizeDto)
sizes!: ProductSizeDto[]
}
I need a DTO for a product update. To avoid repeating myself I inherit from the creation dto. But I need all fields of the parent class to become @IsOptional()
export class UpdateProductDto extends CreateProductDto {
@IsString()
id!: string;
}
Sounds like a use case for
PartialType
from@nestjs/mapped-types
. You would be able to do something likeIf you are using
@nestjs/swagger
you can importPartialType
from@nestjs/swagger
instead to keep the OpenAPI metadata for theUpdateProductsDto
, as it extends the initial implementation of@nestjs/mapped-types