I wrote an api in user.controller using dto of 'class-validator'.
@Post('/')
public async createUser(
@Res() res,
@Body() createUserDto: CreateUserDto,
) {
... do something
}
And I put useGlobalPipes in main.ts.
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
forbidNonWhitelisted: true,
transformOptions: {
enableImplicitConversion: true,
},
}),
);
When I request to the api, I get a response below.
{
"statusCode": 400,
"message": [
"userName should not be empty",
"userName must be a string",
],
"error": "Bad Request"
}
So far, it works without problem with nestjs.
But I want to get a custom response like this.
{
"code": 400,
"errorReason": [
"userName should not be empty",
"userName must be a string",
],
"msg": "Bad Request",
"success": false
}
Can I get this custom response maintaining the code above?
If it can do it, could you give me some advice or some code for this?
Thank you for reading my question.