Validate body
Sorikairox opened this issue · 4 comments
When using @Body
decorator, we should be able to validate the body we are receiving
Using our validatte package, we should validate bodies.
This should be an opt-out feature, we validate by default.
opt-out may look like this :
export const bootstrap = async () => {
const application = new DanetApplication();
application.disableBodyValidation();
await application.init(AppModule);
return application;
}
Upon injecting the body in router.ts
we can validate it, and throws a 400 Bad Request containing validateObject
return value.
I could take this issue. But it isn't totally clear for me what exactly you mean with "validate" the body. I took a look at Body decorator's code and saw that the value of body is extracted inside a try catch block.
The validation would be to change the simple throw e by a handler to return a 400 Bad Request??
Hi @talis-fb , thank you for coming here !
Here is an example of usage for the new feature requested by this issue :
First, we define how our body should look like by defining a class and using our package 'validatte'.
class MyBodyDTO {
@IsString()
@IsEmail()
public email:string;
@IsString()
@LengthGreaterThan(5)
public username: string;
}
The @Body
decorator should, if user has not opt-out the body validation, call 'validatte' function validateObject
on the body to ensure the body is a correct MyBodyDTO
.
If not, it should throw a 400 with detailed error.
Hope this help !
Yeah! It helped me a lot! Thanks for explaination =D. I'll work on the feature!
@talis-fb Cool ! Feel free to join our discord https://discord.gg/Q7ZHuDPgjA