Performance: File validation pipe
MEDIUM IMPACT
This affects the server response time and user experience by validating files before processing, impacting input responsiveness and backend load.
import { PipeTransform, Injectable, BadRequestException, ArgumentMetadata } from '@nestjs/common'; @Injectable() export class FileValidationPipe implements PipeTransform<Express.Multer.File> { async transform(value: Express.Multer.File, metadata: ArgumentMetadata) { if (!value) { throw new BadRequestException('No file provided'); } // Use async checks or offload heavy validation if (value.size > 10 * 1024 * 1024) { throw new BadRequestException('File too large'); } if (!['image/png', 'image/jpeg'].includes(value.mimetype)) { throw new BadRequestException('Invalid file type'); } return value; } }
import { PipeTransform, Injectable, BadRequestException, ArgumentMetadata } from '@nestjs/common'; @Injectable() export class FileValidationPipe implements PipeTransform<Express.Multer.File> { transform(value: Express.Multer.File, metadata: ArgumentMetadata) { if (!value) { throw new BadRequestException('No file provided'); } if (value.size > 10 * 1024 * 1024) { // 10MB limit throw new BadRequestException('File too large'); } if (!['image/png', 'image/jpeg'].includes(value.mimetype)) { throw new BadRequestException('Invalid file type'); } return value; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous file validation in pipe | 0 (server-side) | 0 | 0 | [X] Bad |
| Asynchronous or lightweight validation in pipe | 0 (server-side) | 0 | 0 | [OK] Good |