NestJS:如何在自定义验证器中访问 Body 和 Param?

Car*_*ven 6 javascript validation node.js typescript nestjs

我有一个场景,我需要parambody中的值来执行自定义验证。例如,我有一个/:photoId/tag为照片添加标签的路线。

但是,在为照片添加标签之前,它必须验证是否已经存在与照片同名的标签。

我的控制器中有以下路线:

@Post(':photoId/tag')
@UsePipes(new ValidationPipe())
async addTag(
    @Param() params: AddTagParams,
    @Body() addTagDto: AddTagDto
) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

由于:photoId被设置为PARAMtag在提供身体的要求,他们不能互相访问中自定义验证,我不能使用这两种信息办对数据库进行检查:

export class IsPhotoTagExistValidator implements ValidatorConstraintInterface {

    async validate(val: any, args: ValidationArguments) {
        // supposed to check whether a tag of the same name already exists on photo
        // val only has the value of photoId but not the name of the tag from AddTagDto in Body
    }
}   


export class AddTagParams{
   @IsInt()
   @Validate(IsPhotoTagExistValidator)   // this doesn't work because IsPhotoTagExistValidator can't access tag in AddTagDto
   photoId: number
}

export class AddTagDto{
   @IsString()
   tag: string
}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,valinIsPhotoTagExistValidator只是photoId. 但是我需要photoIdin Param 和tagbody 中的 name 来检查特定的是否photoId已经有了那个tag

我应该如何访问自定义验证器函数中的 Body 和 Param?如果没有,我应该如何解决这个问题?

Yur*_*riv 6

到目前为止我找到的唯一解决方案源自此评论https://github.com/nestjs/nest/issues/528#issuecomment-497020970

context.interceptor.ts

import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common'
import { Observable } from 'rxjs'

/**
 * Injects request data into the context, so that the ValidationPipe can use it.
 */
@Injectable()
export class ContextInterceptor implements NestInterceptor {
  intercept(
    context: ExecutionContext,
    next: CallHandler
  ): Observable<any> {
    const request = context.switchToHttp().getRequest();

    request.body.context = {
      params: request.params,
      query: request.query,
      user: request.user,
    };

    return next.handle()
  }
}
Run Code Online (Sandbox Code Playgroud)

main.ts

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalInterceptors(new ContextInterceptor());
  // ...
}
Run Code Online (Sandbox Code Playgroud)

如果您{whitelist: true}ValidationPipeparams 中使用,则需要允许context在 Dto 对象中使用。

这可以通过扩展这样的 Dto 来完成:

context-aware.dto.ts

import { Allow } from 'class-validator';

export class ContextAwareDto {
  @Allow()
  context?: {
    params: any,
    query: any,
    user: any,
  }
}
Run Code Online (Sandbox Code Playgroud)

之后,您将能够在自定义验证器中验证正文时访问请求数据validationArguments.object.context

您可以轻松调整上述内容以在验证参数或查询时访问上下文,尽管我发现仅在正文验证期间使用此内容就足够了。