0

我有一个自定义类验证器规则:

import { Injectable } from "@nestjs/common";
import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } from "class-validator";

@ValidatorConstraint({ name: "Foo", async: true })
@Injectable()
export class FooRule implements ValidatorConstraintInterface {    
    async validate(value: unknown, args: ValidationArguments) {
        // how to access request object from here?
    }
    
    defaultMessage(args: ValidationArguments) {
        return "NOT OK.";
    }
}

如何访问validate()方法内的请求对象?

4

1 回答 1

0

我最终使用了一个自定义拦截器:

import { Injectable, NestInterceptor, CallHandler, ExecutionContext } from "@nestjs/common";
import { GqlExecutionContext } from "@nestjs/graphql";
import { ForbiddenError } from "apollo-server-core";
import { Observable } from "rxjs";

@Injectable()
export class FooInterceptor implements NestInterceptor {
    intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        // get gql execution context from http one
        const gqlCtx = GqlExecutionContext.create(context);

        // holds data passed in a gql input
        const args: unknown[] = gqlCtx.getArgs();

        // req object (can be used to obtain jwt payload)
        const req = gqlCtx.getContext().req;

        // validation logic here
        const validationPassed = true;

        if (validationPassed) {
            // invoke the route handler method 
            return next.handle();
        }

        // will be caught by nest exceptions layer
        throw new ForbiddenError("Not allowed.");
    }
}
于 2022-03-03T16:19:14.043 回答