fix: ensure nested objects are properly validated & transformed

To validate nested objects, class-transformer requires
the `@ValidateNested` annotation.
For arrays, class-transfomer requires
setting `each: true`.

To correctly transform nested objects from JSON to instances,
class-transformer requires the `@Type` annotation.

References:
https://github.com/typestack/class-validator#validating-nested-objects
https://github.com/typestack/class-validator#validating-arrays
https://github.com/typestack/class-transformer#working-with-nested-objects
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-03-05 12:02:13 +01:00 committed by Philip Molares
parent 324536bc2d
commit 59a235ebc4
3 changed files with 16 additions and 7 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
IsArray,
IsDate,
@ -104,14 +105,15 @@ export class NoteMetadataDto extends BaseDto {
* @example "['john.smith', 'jane.smith']"
*/
@IsArray()
@ValidateNested()
@IsString({ each: true })
@ApiProperty()
editedBy: UserInfoDto['username'][];
editedBy: string[];
/**
* Permissions currently in effect for the note
*/
@ValidateNested()
@ValidateNested({ each: true })
@Type(() => NotePermissionsDto)
@ApiProperty({ type: NotePermissionsDto })
permissions: NotePermissionsDto;
}