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 like
import { PartialType } from '@nestjs/mapped-types';
export class UpdateProductDto extends PartialType(CreateProductDto) {
@IsString()
id!: string;
}
If you are using @nestjs/swagger
you can import PartialType
from @nestjs/swagger
instead to keep the OpenAPI metadata for the UpdateProductsDto
, as it extends the initial implementation of @nestjs/mapped-types