简体   繁体   English

如何使用 GraphQL 在 NestJS 中创建自定义字段验证装饰器

[英]How to create custom field validation decorators in NestJS with GraphQL

I'm working on a NestJS API with apollo-server-express and I have the next InputType for appointments:我正在使用 apollo-server-express 开发 NestJS API,我有下一个 InputType 用于约会:

@InputType()
export class AppointmentInput {
    @Field(of => String)
    @IsNotEmpty()
    name: string;

    @Field(of => String)
    @IsNotEmpty()
    @IsDateString()
    dateStart: string;

    @Field(of => String)
    @IsNotEmpty()
    @IsDateString()
    dateEnd: string;

    @Field(of => Boolean)
    @IsBoolean()
    paid: boolean;

    @Field(of => Int)
    idDoctor: number;
    
    @Field(of => Int)
    idPatient: number;
    @Field(of => Int)
    idService: number;
}

Let's say I want to validate if the name has some "a" letter with a Pipe:假设我想验证该名称是否有一些带有 Pipe 的“a”字母:

import { PipeTransform, Injectable, ArgumentMetadata, HttpException, HttpStatus } from '@nestjs/common';

@Injectable()
export class NamePipe implements PipeTransform<string, string>{
  transform(name: string, metadata: ArgumentMetadata) {
    if (name.includes('a')) {
      throw new HttpException('The name has an a', HttpStatus.BAD_REQUEST);
    }
    return name;
  }
}

But if use it like this:但是如果像这样使用它:

@Field(of => String)
@IsNotEmpty()
@UsePipes(NamePipe)
name: string;

It throws me the next error:它抛出下一个错误:

The return type of a property decorator function must be either 'void' or 'any'.属性装饰器 function 的返回类型必须是“void”或“any”。 Unable to resolve signature of property decorator when called as an expression.当作为表达式调用时,无法解析属性装饰器的签名。 Type 'TypedPropertyDescriptor' is not assignable to type 'void'.类型“TypedPropertyDescriptor”不可分配给类型“void”。

How can I solve this and use custom decorators to validate my fields?我该如何解决这个问题并使用自定义装饰器来验证我的字段? Also is important that my decorators can call services, because maybe in the future I want to validate for example the role id, and check if in the database exists a role with that id and if not throw an error.我的装饰器可以调用服务也很重要,因为也许将来我想验证例如角色 ID,并检查数据库中是否存在具有该 ID 的角色,如果不存在则抛出错误。

I think you should rather create a custom class-validator decorator and use it like any class-validator decorator, assuming you've already applied a global NestJS validation pipe我认为您应该创建一个自定义类验证器装饰器并像使用任何类验证器装饰器一样使用它,假设您已经应用了全局 NestJS 验证 pipe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NestJS 的自定义路由装饰器 - Custom Route Decorators for NestJS 当字段应该是可选的时,带有 InterestionType/PickType 的 NestJS DTO 装饰器仍然强制执行 isDefined() 验证 - NestJS DTO Decorators with InterestionType/PickType still enforcing isDefined() validation when field should be optional Nestjs 验证 pipe 不适用于 Graphql - Nestjs validation pipe not working with Graphql 如何将自定义标量导入NestJs中的.graphql文件? - How do I import a custom scalar into a .graphql file in NestJs? Graphql Nestjs 将 arguments 添加到类型字段 - Graphql Nestjs add arguments to type field 如何使用自定义返回类型在 type-graphql 中创建 GraphQL 查询 - How to create a GraphQL query in type-graphql with a custom return type Nestjs 扩展/组合装饰器? - Nestjs extend/combine decorators? NestJS:如何从 JWT AuthGuard 扩展的 GraphQL 自定义 Guard 中的请求中获取用户 - NestJS: How to get User from the request in a GraphQL custom Guard which extends from JWT AuthGuard 如何在 Nestjs 的 Resolve Decorator 中创建两个或多个 Resolve 字段? - How to create two or more Resolve Field in Resolve Decorator in Nestjs? 如何使用 [nestjs/graphql] 在解析器中实现搜索 - How to implement a search in a resolver using [nestjs/graphql]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM