Nima Soufiloo
Nima Soufiloo

Reputation: 254

Validate response format in NestJs

I want to validate responses in NestJs so they can have strictly formatted.

for example when getting users from the database the password field shouldn't be in the response object. I could delete the password property in users before sending them but I'm wondering is there any way to validate response object and have them transformed before sending them to the client ?

in this case transform means deleting any extra properties like password automatically by using something like DTO and class-validator / class-transformer.

Upvotes: 1

Views: 4154

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70211

Sounds like you're wanting to use Nest's ClassSerializationInterceptor. So long as the object you return from your controller is a class instance decorated with class-transformer decorators, the interceptor will run classToPlain() on it, and put the result back in plain JSON, stripping anything you tell it to as well.

Upvotes: 2

Related Questions