refactor(api/private/history): validate POST data with DTO

This adds a `HistoryEntryImportListDto` which allows
to fully validate incoming JSON data.

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-03-04 18:01:08 +01:00
parent a32d9e8305
commit fd3fde9cc8
2 changed files with 17 additions and 4 deletions

View file

@ -15,7 +15,7 @@ import {
} from '@nestjs/common'; } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger'; import { ApiTags } from '@nestjs/swagger';
import { HistoryEntryImportDto } from '../../../../history/history-entry-import.dto'; import { HistoryEntryImportListDto } from '../../../../history/history-entry-import.dto';
import { HistoryEntryUpdateDto } from '../../../../history/history-entry-update.dto'; import { HistoryEntryUpdateDto } from '../../../../history/history-entry-update.dto';
import { HistoryEntryDto } from '../../../../history/history-entry.dto'; import { HistoryEntryDto } from '../../../../history/history-entry.dto';
import { HistoryService } from '../../../../history/history.service'; import { HistoryService } from '../../../../history/history.service';
@ -53,9 +53,9 @@ export class HistoryController {
@OpenApi(201, 404) @OpenApi(201, 404)
async setHistory( async setHistory(
@RequestUser() user: User, @RequestUser() user: User,
@Body('history') history: HistoryEntryImportDto[], @Body() historyImport: HistoryEntryImportListDto,
): Promise<void> { ): Promise<void> {
await this.historyService.setHistory(user, history); await this.historyService.setHistory(user, historyImport.history);
} }
@Delete() @Delete()

View file

@ -4,7 +4,13 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { Type } from 'class-transformer'; import { Type } from 'class-transformer';
import { IsBoolean, IsDate, IsString } from 'class-validator'; import {
IsArray,
IsBoolean,
IsDate,
IsString,
ValidateNested,
} from 'class-validator';
import { BaseDto } from '../utils/base.dto.'; import { BaseDto } from '../utils/base.dto.';
@ -28,3 +34,10 @@ export class HistoryEntryImportDto extends BaseDto {
@Type(() => Date) @Type(() => Date)
lastVisited: Date; lastVisited: Date;
} }
export class HistoryEntryImportListDto extends BaseDto {
@ValidateNested({ each: true })
@IsArray()
@Type(() => HistoryEntryImportDto)
history: HistoryEntryImportDto[];
}