#Validation Nested Objects
7 messages · Page 1 of 1 (latest)
Please format your question with Markdown formatting.
It leads to better readability and an easier time to spot problems.
For code blocks, you can wrap your block with three back ticks before and after the block, and after the first three back ticks you can add a language (like ts) to add syntax highlighting.
e.g.
```ts
@Injectable()
export class MySuperAwesomeService {
constructor(@Inject('InjectionToken') private readonly dep: SomeDependency) {}
getRandomNumber(): number {
return Math.round(Math.random() * 1000);
}
}
```
Becomes
@Injectable()
export class MySuperAwesomeService {
constructor(@Inject('InjectionToken') private readonly dep: SomeDependency) {}
getRandomNumber(): number {
return Math.round(Math.random() * 1000);
}
}
export class FinancialAspect{
@ApiProperty()
@Type(() => Number)
@IsNumber()
bp: number;
@ApiProperty()
@Type(() => Number)
@IsNumber()
rab: number;
@ApiProperty()
@Type(() => Number)
@IsNumber()
pfk: number;
@ApiProperty()
@Type(() => Number)
@IsNumber()
spp: number;
}
export class SurveyDTO{
@ApiProperty({
description: 'financial aspect',
})
@IsNotEmptyObject()
@ValidateNested()
@Type(() => FinancialAspectOptional)
financialAspect?: FinancialAspect;
}
export class OptionalSurveyDTO extends
PartialType(SurveyDTO) {}
How to make all fields in FinancialAspect optional when using OptionalSurveyDTO?
You can use @IsOptional decorator. If you also want to reflect that in swagger, use @ApiPropertyOptional
I've tried that before and not working. I found another approach. I create FinancialAspectOptional and override it in OptionalSurveyDTO
export class FinancialAspectOptional extends PartialType(FinancialAspect) {}
export class OptionalSurveyDTO extends PartialType(SurveyDTO) {
@Transform(({value}) => {
return plainToClass(FinancialAspectOptional, value)
financialAspect: FinancialAspect;
}