Nestjs 类转换器和类验证器:在类验证器错误中显示@exposed name
我有一个NestJS项目,其中我同时使用类验证器和类转换器,我需要在类验证器抛出错误之前执行类转换器 鉴于以下类别:Nestjs 类转换器和类验证器:在类验证器错误中显示@exposed name,nestjs,class-validator,class-transformer,Nestjs,Class Validator,Class Transformer,我有一个NestJS项目,其中我同时使用类验证器和类转换器,我需要在类验证器抛出错误之前执行类转换器 鉴于以下类别: 导出类CreateProfileDto{ @IsString() @公开({name:'name'}) profileName!:字符串; @IsBoolean() @公开({name:'活动'}) profileActive!:布尔值; } 我需要用属性名而不是属性profileName公开错误,对其他属性也是如此 有什么直截了当的办法来处理这个问题吗?无法要求前端将不同名称
导出类CreateProfileDto{
@IsString()
@公开({name:'name'})
profileName!:字符串;
@IsBoolean()
@公开({name:'活动'})
profileActive!:布尔值;
}
我需要用属性名而不是属性profileName公开错误,对其他属性也是如此
有什么直截了当的办法来处理这个问题吗?无法要求前端将不同名称的属性发送给我,这就是为什么我需要调整它们
我本想通过管道来实现,但在错误爆发之前无法使用它
当前错误格式:
{
"statusCode": 400,
"error": "Bad Request",
"message": [
{
"target": {
"profileName": 1
},
"value": 1,
"property": "profileName",
"children": [],
"constraints": {
"isString": "profileName must be a string"
}
}
]
}
{
"statusCode": 400,
"error": "Bad Request",
"message": [
{
"target": {
"name": 1
},
"value": 1,
"property": "name",
"children": [],
"constraints": {
"isString": "name must be a string"
}
}
]
}
所需的错误格式:
{
"statusCode": 400,
"error": "Bad Request",
"message": [
{
"target": {
"profileName": 1
},
"value": 1,
"property": "profileName",
"children": [],
"constraints": {
"isString": "profileName must be a string"
}
}
]
}
{
"statusCode": 400,
"error": "Bad Request",
"message": [
{
"target": {
"name": 1
},
"value": 1,
"property": "name",
"children": [],
"constraints": {
"isString": "name must be a string"
}
}
]
}
您可以通过上下文将属性名传递给decorator。看这里: