node.js - 一起使用两个 dto 类时不发送错误消息
问题描述
我有一个范围 dto 类:
export class UpdateProgramScopeDto {
@Matches(scopeTypeRegex)
@ApiProperty({
type: String,
description: 'scopeType',
enum: SCOPE_TYPE,
})
scopeType: SCOPE_TYPE;
}
以及在另一个类中使用它:
export class RegisterProgramDto {
@IsArray()
@ValidateNested({ message: 'd' })
@Type(() => UpdateProgramScopeDto)
@ApiProperty({
type: UpdateProgramScopeDto,
description: 'scopes',
isArray: true,
})
scopes: UpdateProgramScopeDto[];
}
但是未发送范围类的错误消息我该如何解决?
@Post()
async registerProgram(
@Body(ValidationPipe) registerProgramDto: RegisterProgramDto,
@User() user: AuthEntity,
) {
return await this._programService.registerProgram(
user.user_id,
registerProgramDto,
);
}
解决方案
如果需要验证对象数组,请使用each: true
in @ValidateNested({ each: true })
。
在 NestJS 中,您还需要Type
from class-transform
。这是一个工作示例:
export class Foo {
@ValidateNested({ each: true })
@Type(() => Bar)
posts: Bar[];
}
推荐阅读
- php - wp 使用 ACF 关系按成分类别过滤食谱
- ios - 我想在单击单元格时更改集合视图单元格的图像
- reactjs - 如何使用 Github Actions 保存构建文件(React)?
- java - 如何解决此计算器错误?我是初学者
- python - 并非所有字段都在 django admin 中正确显示
- python - 我如何将这些数字写入其中的列表,因为我尝试使用 for 循环但它不起作用并将其写入名为 list_of_numbers 的列表
- biginteger - 不调用大量函数
- firebase - Flutter Web NoSuchMethodError 上的 Firebase 火库。“文档”
- flutter - 为什么滚动直到最后都无法正常工作
- regex - 如何保留您在 Notepad++ 中替换的部分文本