I have a NestJS project in which I'm using both class validator and class transformer, and I need the class-transformer to execute before the class-validator throws an error.
Given the following class:
export class CreateProfileDto {
@IsString()
@Expose({ name: 'name' })
profileName!: string;
@IsBoolean()
@Expose({ name: 'active'})
profileActive!: boolean;
}
I need to expose the errors with the property name and not the property profileName and the same goes for the other property.
Any straight forward idea to manage this? Can't ask the frontend to send me the properties with different names, that's why I need to adjust to them.
I thought to do so through a pipe but can't manage to use it before the error explodes.
Current error format:
{
"statusCode": 400,
"error": "Bad Request",
"message": [
{
"target": {
"profileName": 1
},
"value": 1,
"property": "profileName",
"children": [],
"constraints": {
"isString": "profileName must be a string"
}
}
]
}
Desired error format:
{
"statusCode": 400,
"error": "Bad Request",
"message": [
{
"target": {
"name": 1
},
"value": 1,
"property": "name",
"children": [],
"constraints": {
"isString": "name must be a string"
}
}
]
}