我们有一个 DTO 属性,其类型为
Record<string, Animal>
,而 Animal
是另一个带有 @IsDefined()、@IsNotEmpty() 等类验证器的 DTO。然而,这些验证没有按预期工作,可能是因为它具有 Record 类型。
我已经尝试添加
@ValidatedNested({ each: true })
但仍然不起作用。我们如何才能对 Animal
中的 Record<string, Animal>
dto 进行验证?
这是我遇到的唯一可行的解决方案。已经测试过自己并且像魔术一样工作:https://github.com/nestjs/swagger/issues/1771#issuecomment-1480873230
嗨 您可以尝试构建部分类验证器,https://trilon.io/blog/introducing-mapped-types-for-nestjs这里有一些文档。 例如:
export class Record extends PartialType(
OmitType(Animal , ['legs'] as const),
) {}