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

View file

@ -4,7 +4,13 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
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.';
@ -28,3 +34,10 @@ export class HistoryEntryImportDto extends BaseDto {
@Type(() => Date)
lastVisited: Date;
}
export class HistoryEntryImportListDto extends BaseDto {
@ValidateNested({ each: true })
@IsArray()
@Type(() => HistoryEntryImportDto)
history: HistoryEntryImportDto[];
}