【发布时间】:2021-02-23 05:14:55
【问题描述】:
我的 NestJS 控制器中有以下 DTO 对象作为请求正文的一部分:
export class UserPropertiesDto {
[key: string]: boolean;
}
例如:{campaignActive: true, metadataEnabled: false}
这是一个键值对object,其中键是唯一的string,其值为boolean。
我想应用class-validator 注释以确保正确的验证和转换,但它一直显示错误Decorators are not valid here:
export class UserPropertiesDto {
@IsOptional()
@IsString() // `key` should be a string
@MaxLength(20) // `key` should have no more than 20 characters
@IsBoolean() // `value` has to be a `boolean`
[key: string]: boolean;
}
您能否就最好的方法提出建议:
- 确保保留所有对象的属性
- 验证密钥以确保它是长度不超过 20 个字符的字符串
- 验证值以确保它是
boolean
【问题讨论】:
标签: javascript typescript nestjs dto class-validator