0
0
NestJSframework~10 mins

File validation pipe in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the correct decorator for file upload in NestJS.

NestJS
import { [1] } from '@nestjs/common';
Drag options to blanks, or click blank then click option'
AUploadedFile
BUseInterceptors
CController
DGet
Attempts:
3 left
💡 Hint
Common Mistakes
Using @UseInterceptors instead of @UploadedFile
Importing from wrong package
Confusing with @Controller or @Get decorators
2fill in blank
medium

Complete the code to apply a file validation pipe in the controller method parameter.

NestJS
uploadFile(@UploadedFile(new [1]()) file: Express.Multer.File) { return file; }
Drag options to blanks, or click blank then click option'
AFileValidationPipe
BValidationPipe
CParseFilePipe
DParseIntPipe
Attempts:
3 left
💡 Hint
Common Mistakes
Using ValidationPipe which does not handle files
Using a non-existent FileValidationPipe
Using ParseIntPipe which is for numbers
3fill in blank
hard

Fix the error in the pipe options to allow only PNG files.

NestJS
new ParseFilePipe({ validators: [ new FileTypeValidator({ fileType: '[1]' }) ] })
Drag options to blanks, or click blank then click option'
Aimage/jpeg
Bimage/png
Capplication/pdf
Dtext/plain
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'image/jpeg' which allows JPEG files
Using 'application/pdf' which allows PDFs
Using 'text/plain' which allows text files
4fill in blank
hard

Fill both blanks to set a max file size of 1MB and allow only JPEG files.

NestJS
new ParseFilePipe({ validators: [ new FileTypeValidator({ fileType: '[1]' }), new MaxFileSizeValidator({ maxSize: [2] }) ] })
Drag options to blanks, or click blank then click option'
Aimage/jpeg
B1048576
Cimage/png
D2097152
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'image/png' instead of 'image/jpeg'
Setting maxSize to 2097152 which is 2MB
5fill in blank
hard

Fill all three blanks to create a custom validation pipe that checks file size and type.

NestJS
export class CustomFileValidationPipe implements PipeTransform {
  transform(file: Express.Multer.File) {
    if (!file) throw new BadRequestException('No file uploaded');
    if (file.mimetype !== '[1]') throw new BadRequestException('Invalid file type');
    if (file.size > [2]) throw new BadRequestException('File too large');
    return file;
  }
}

// Usage: @UploadedFile(new [3]())
Drag options to blanks, or click blank then click option'
Aimage/png
B1048576
CCustomFileValidationPipe
Dimage/jpeg
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'image/png' instead of 'image/jpeg'
Using wrong max size value
Not using the custom pipe class name in the decorator