I am using the NestJs framework (love it by the way) and I want to check the incoming data so it conforms with an Enum in Typscript. So I have the following:
enum ProductAction { PURCHASE = 'PURCHASE', } @Patch('products/:uuid') async patchProducts( @Param('uuid', ParseUUIDPipe) uuid: string, @Body('action', ParseEnumPipe) action: ProductAction, ) { switch(action) { ... code }
The weird thing is that when I run this code, the first pipe gets compiled
2022-07-21 16:53:51 [error] [ExceptionHandler] Nest can't resolve dependencies of the ParseEnumPipe (?, Object). Please make sure that the argument Object at index [0] is available in the FriendsModule context.
What I am doing wrong?
Advertisement
Answer
You should use @Body('action', new ParseEnumPipe(ProductAction)) action: ProductAction
because enums aren’t directly reflected for Nest to read the metadata of, and because Nest is otherwise trying to figure out how to inject Object
when it really should be injecting the enum.