custom object validation not working in nestjs DTO

Viewed 3611

NestJs DTO does not validate object properly

DTO class enter image description here

Controller Class

enter image description here

main.ts I also try adding { whitelist: true } and {forbidNonWhitelisted: true } and { whitelist: true, forbidNonWhitelisted: true } but no luck

enter image description here

This is how I call my API and this type response is ok

enter image description here

If I passed the wrong properties it not throw an error...This is the issue, It should be only working if I pass valid properties. enter image description here

Thanks.

1 Answers

Your GeoPint class has no validations on it, so the body can be trasformed to it, but there's nothing to validate on it, so yeah, there's no errors. You can make this stricter with a couple of options like forbidNonWhitelisted and forbidUnknownValues, but as is this is working as expected. Add some class-validator decorators to the GeoPint class and you'll see it start validating the data. Add forbidNonWhitelisted and you'll see it reject unknown properties.

Related