feat(dto): enforce field length constraints across DTOs

Add `@MaxLength` validations to limit string field lengths in multiple DTOs, ensuring consistent data validation and integrity. Integrate `CreateApiKeyDto` in the API keys controller for improved type safety.
This commit is contained in:
Mathis HERRIOT
2026-01-14 20:41:45 +01:00
parent 5f2672021e
commit 5671ba60a6
7 changed files with 34 additions and 7 deletions

View File

@@ -11,6 +11,7 @@ import {
import { AuthGuard } from "../auth/guards/auth.guard";
import type { AuthenticatedRequest } from "../common/interfaces/request.interface";
import { ApiKeysService } from "./api-keys.service";
import { CreateApiKeyDto } from "./dto/create-api-key.dto";
@Controller("api-keys")
@UseGuards(AuthGuard)
@@ -20,13 +21,12 @@ export class ApiKeysController {
@Post()
create(
@Req() req: AuthenticatedRequest,
@Body("name") name: string,
@Body("expiresAt") expiresAt?: string,
@Body() createApiKeyDto: CreateApiKeyDto,
) {
return this.apiKeysService.create(
req.user.sub,
name,
expiresAt ? new Date(expiresAt) : undefined,
createApiKeyDto.name,
createApiKeyDto.expiresAt ? new Date(createApiKeyDto.expiresAt) : undefined,
);
}

View File

@@ -0,0 +1,12 @@
import { IsDateString, IsNotEmpty, IsOptional, IsString, MaxLength } from "class-validator";
export class CreateApiKeyDto {
@IsString()
@IsNotEmpty()
@MaxLength(128)
name!: string;
@IsOptional()
@IsDateString()
expiresAt?: string;
}

View File

@@ -1,15 +1,18 @@
import { IsNotEmpty, IsOptional, IsString } from "class-validator";
import { IsNotEmpty, IsOptional, IsString, MaxLength } from "class-validator";
export class CreateCategoryDto {
@IsString()
@IsNotEmpty()
@MaxLength(64)
name!: string;
@IsOptional()
@IsString()
@MaxLength(255)
description?: string;
@IsOptional()
@IsString()
@MaxLength(512)
iconUrl?: string;
}

View File

@@ -6,6 +6,7 @@ import {
IsOptional,
IsString,
IsUUID,
MaxLength,
} from "class-validator";
export enum ContentType {
@@ -19,14 +20,17 @@ export class CreateContentDto {
@IsString()
@IsNotEmpty()
@MaxLength(255)
title!: string;
@IsString()
@IsNotEmpty()
@MaxLength(512)
storageKey!: string;
@IsString()
@IsNotEmpty()
@MaxLength(128)
mimeType!: string;
@IsInt()
@@ -39,5 +43,6 @@ export class CreateContentDto {
@IsOptional()
@IsArray()
@IsString({ each: true })
@MaxLength(64, { each: true })
tags?: string[];
}

View File

@@ -4,6 +4,7 @@ import {
IsOptional,
IsString,
IsUUID,
MaxLength,
} from "class-validator";
import { ContentType } from "./create-content.dto";
@@ -13,6 +14,7 @@ export class UploadContentDto {
@IsString()
@IsNotEmpty()
@MaxLength(255)
title!: string;
@IsOptional()
@@ -20,6 +22,8 @@ export class UploadContentDto {
categoryId?: string;
@IsOptional()
@IsArray()
@IsString({ each: true })
@MaxLength(64, { each: true })
tags?: string[];
}

View File

@@ -1,4 +1,4 @@
import { IsEnum, IsOptional, IsString, IsUUID } from "class-validator";
import { IsEnum, IsOptional, IsString, IsUUID, MaxLength } from "class-validator";
export enum ReportReason {
INAPPROPRIATE = "inappropriate",
@@ -21,5 +21,6 @@ export class CreateReportDto {
@IsOptional()
@IsString()
@MaxLength(1000)
description?: string;
}

View File

@@ -1,11 +1,13 @@
import { IsNotEmpty, IsString } from "class-validator";
import { IsNotEmpty, IsString, MaxLength } from "class-validator";
export class UpdateConsentDto {
@IsString()
@IsNotEmpty()
@MaxLength(16)
termsVersion!: string;
@IsString()
@IsNotEmpty()
@MaxLength(16)
privacyVersion!: string;
}