Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 带有服务层的NESTJS验证管道和CustomValidatorDecorator_Typescript_Nestjs - Fatal编程技术网

Typescript 带有服务层的NESTJS验证管道和CustomValidatorDecorator

Typescript 带有服务层的NESTJS验证管道和CustomValidatorDecorator,typescript,nestjs,Typescript,Nestjs,你好,我最近刚接触NESTJS,我需要帮助解决这个问题。问题在于,我需要在验证管道上访问请求的令牌以调用另一个api(使用相同的令牌),并使用HttpModule检查发送的id是否有效 验证管道(来自nestjs文档的样本): 这样做的目的是,在DTO上,我将decorators放入验证,然后他自动调用api来检查语言id是否存在 @ApiModelProperty() @IsLanguageValid({}, { message: 'the language id sent is no

你好,我最近刚接触NESTJS,我需要帮助解决这个问题。问题在于,我需要在验证管道上访问请求的令牌以调用另一个api(使用相同的令牌),并使用HttpModule检查发送的id是否有效

验证管道(来自nestjs文档的样本):

这样做的目的是,在DTO上,我将decorators放入验证,然后他自动调用api来检查语言id是否存在

  @ApiModelProperty()
  @IsLanguageValid({}, { message: 'the language id sent is not valid'})
  @IsNumber({}, { message: 'the id should be a number' })
  language_id: number;
import { registerDecorator, ValidationOptions, ValidationArguments } from 'class-validator';

export function IsLanguageValid(property: string, validationOptions?: ValidationOptions) {
  return function(object: Object, propertyName: string) {
    registerDecorator({
      name: 'isLanguageValid',
      target: object.constructor,
      propertyName: propertyName,
      constraints: [property],
      options: validationOptions,
      validator: {
        validate(value: any, args: ValidationArguments) {
          const [relatedPropertyName] = args.constraints;
          const relatedValue = (args.object as any)[relatedPropertyName];

          // WHERE HOW CAN I ACCESS TO A SERVICE AND TOKEN OF THE REQUEST
          this.httpModule.get('url to other api', { headers: { authrorization : TOKEN }})
        },
      },
    });
  };
}
  @ApiModelProperty()
  @IsLanguageValid({}, { message: 'the language id sent is not valid'})
  @IsNumber({}, { message: 'the id should be a number' })
  language_id: number;