Validation pipe adds missing properties to the dto object

Viewed 422

I am using NestJS. When I used node version 12.13, my data transfer objects had optional fields and validation pipe would just skip them. However, after I updated to node 12.20, if request body corresponding to dto doesn't contain some property, pipe will automatically add this property to object and set it as undefined, which can cause some problems later in my code. Is there a way to avoid this?

1 Answers

This is a side effect of class-transformer to whom your DTO gets passed during validation. You can re-activate the original behavior by passing the following option to your ValidationPipe.

new ValidationPipe({ transformOptions: { exposeUnsetFields: false } })
Related