ruciu
ruciu

Reputation: 712

Validating files in validation pipes in Nest.js

I would like to validate files which are uploaded in request with @Pipe(). Currently when I do

@UsePipes(new MyValidationPipe())
@Post()
public async addDocument(@Body() body, @Req() { user, files }: {user: IUserDocument, files: IImagesFromRequest}):
Promise<IDocument> {
    return this.surveyService.createDocument(body, user, files);
}

I have only @Body() body in my pipe. However I also need @Req() files to check if those are correct as well. I know I could check them using @Middleware(), but I feel it would be inconsistent to have validation done in two different ways. Is there any way to achieve what I need using @Pipe()? Or maybe should I take differnet approach.

Thanks in advance

Upvotes: 2

Views: 6173

Answers (1)

Kamil Myśliwiec
Kamil Myśliwiec

Reputation: 9168

You can create your own decorator https://docs.nestjs.com/custom-decorators. The pipes are running for custom decorators as well, thus it should fit your requirements.

Upvotes: 1

Related Questions