【发布时间】:2020-06-23 18:07:32
【问题描述】:
我正在尝试编写一个自定义验证装饰器,该装饰器将禁止用户在单个类别/子类别中创建具有相同标题的条目。为此,我将使用类验证器库并编写如下装饰器:
@ValidatorConstraint({ name: 'isUniqueEntryTitle', async: true })
@Injectable()
export class IsUniqueEntryTitle implements ValidatorConstraintInterface {
constructor(
private readonly userService: UserService,
) {}
public async validate(val: any, args: ValidationArguments): Promise<boolean> {
return true;
}
public defaultMessage(args: ValidationArguments): string {
return `Entry with such title already exists in this folder`;
}
}
问题在于,我将不得不进行一个非常复杂的数据库查询来检查具有这种标题的条目是否已经在数据库中。我需要知道的参数之一显然是用户 ID,用户 ID 可以从用户 jwt 令牌中检索,但我如何在此类中访问它?这就是我现在遇到的问题。
【问题讨论】:
标签: typescript nestjs