digicolp
digicolp

Reputation: 53

Validation Pipes in NestJS : @UsePipes(ValidationPipe) or @UsePipes(new ValidationPipe())?

I previously saw these two syntaxes for UsePipes Decorator in NestJS tutorials:

@UsePipes(ValidationPipe) 
@UsePipes(new ValidationPipe())

From what I understand, ValidationPipe is a class itself, where new ValidationPipe() just works as in all object-oriented languages and creates a new instance of ValidationPipe.

What should I use ? What is the difference ? Is there any special usage where one if preferable to another ?

Upvotes: 1

Views: 1011

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70191

Both work fine as is. If you need to pass any options to the pipe though, you should use the new ValidationPipe(). Otherwise, Nest will see the pipe is a class reference, not an instance, and create the instance to be used.

Upvotes: 4

Related Questions