如何在 guard(Resource OwnerGuard) 之前执行 pipe(Validate Object Id)?

How to execute pipe(ValidateObjectId) before guard(ResourceOwnerGuard)?

我在玩 nestjs 和 mongoose。

代码:

class BrevesController {

    constructor(private readonly brevesService: BrevesService) { }
     // Here is used BreveOwnerGuard(1)
    @UseGuards(JwtAuthGuard, BreveOwnerGuard)
    @Get(':breveId')
    // Here is used ValidateObjectId(3)
    async getById(@Param('breveId', ValidateObjectId) id: string) {
        return await this.brevesService.getById(id)
    }
}

class BreveOwnerGuard {

    constructor(private readonly brevesService: BrevesService) { }

    async canActivate(context: ExecutionContext) {
        const req = context.switchToHttp().getRequest()
        const {user, params} = req
        const {breveId} = params
        // This is executed before ValidateObjectId in getById 
        // route handler and unknown error is thrown but we
        // have pipe for this.(2)
        const breve = await this.brevesService.getById(breveId)
        const breveCreatorId = breve.creatorId.toString()
        const userId = user.id
        return breveCreatorId === userId
    }
}

因此,在使用无效对象 ID 请求 /breves/:breveId 后,BreveOwnerGuard 在 ValidateObjectId 之前执行,并抛出未知错误。

这个流程有没有办法在 BreveOwnerGuard 之前验证 ObjectId?

或者这种情况我该怎么办?预期是什么?

Guards are executed after each middleware, but before any interceptor or pipe.

除了将 ResourceOwnerGuard 更改为管道或将 ValidateObjectId 更改为守卫外,您无能为力。