关于node.js:实现Nest中参数的联合类型校验

11次阅读

共计 4148 个字符,预计需要花费 11 分钟才能阅读完成。

前言

在 nest 的 dto 层对参数进行校验时,某个参数可能有多种类型,遇到这种状况你会怎么解决?本文将跟大家分享这个问题的解决方案,欢送各位感兴趣的开发者浏览本文。

场景概述

咱们在进行接口开发时,客户端须要传入一个名为 text 的字段,它可能是 string 类型或 Array<Object> 类型(在 TS 中咱们把这种关系称之为 联结类型 ),class-validator 库中提供了相干的校验注解,那把他们写在一起是否实现相干的校验呢,如下所示:

export class AppDto {@ApiProperty({ example: "2022 年 4 月 20 日批改", description: "备注"})
  @IsString()
  @IsArray()
  @ValidateNested({each: true})
  @Type(() => TextObjDto)
  public text!: string | Array<TextObjType>; 
}

TextObjDto 的代码如下所示:

export class TextObjDto {@ApiProperty({ example: "修复了一些 bug", description: "内容"})
  @IsString()
  content!: string;
  @ApiProperty({example: "2022-04-20 07:52", description: "创立工夫"})
  @IsString()
  createTime?: string;
  @ApiProperty({example: true, description: "是否为新性能标识"})
  @IsBoolean()
  mark?: boolean;
}

启动我的项目,用 postman 测试后发现并不好使,传了 array 类型的数据又要求是 string 类型,传了 string 类型的数据又要求是 array 类型。

留神:嵌套类型的对象验证须要应用 @ValidateNested 和 @Type 注解,@Type 承受一个回调函数,函数外部须要返回一个用 class 申明的 dto 类。

解决方案

通过一番求助,翻了一圈 class-validator 的文档,发现没有现成的解决方案。那么,就只能本人拿到参数搞自定义校验了。

class-transformer 这个库中,提供了 Transform 办法,它承受一个回调函数作为参数,回调函数中提供了一个 TransformFnParams 类型的参数,其中的 value 字段就是客户端传过来的参数,咱们只须要对其进行校验即可。

接下来,咱们来看下实现代码,如下所示:

export class AppDto {@ApiProperty({ example: "2022 年 4 月 20 日批改", description: "备注"})
  @IsOptional()
  @Transform(({value}) => checkTitleKey(value))
  public text!: string | Array<TextObjType>;
}

上述代码中,咱们有一个名为 checkTitleKey 的校验函数,因为须要本人校验,所以就须要本人把 TS 的类型校验复刻一遍进去,实现代码如下所示:

  • 如果校验通过间接返回 value 参数即可
  • 如果校验不通过间接应用 nest 内置异样进行抛出即可
export function checkTitleKey(value: string | number | Array<TextObjType> | undefined | null): any {if (typeof value === "string") {
    // 不做更改,间接返回
    return value;
  } else if (value instanceof Array) {
    // 不能为空数组
    if (value.length <= 0) {
      throw new BadRequestException(
        "property text cannot be an empty array",
        "Bad Request"
      );
    }
    for (let i = 0; i < value.length; i++) {
      // 校验数组中的对象字段
      const objKeys = Object.keys(value[i]);
      if (objKeys.length <= 0) {
        throw new BadRequestException(
          "property text contains empty objects",
          "Bad Request"
        );
      }
      // 必须蕴含 content 字段
      if (!objKeys.includes("content")) {
        throw new BadRequestException("property text objects in the array must contain'content'","Bad Request");
      }
      // 对每个 key 进行校验
      for (let j = 0; j < objKeys.length; j++) {switch (objKeys[j]) {
          case "content":
            // content 字段必须为 string 类型
            if (typeof value[i].content !== "string") {
              throw new BadRequestException(
                "property text'content'of the objects in the array must be of type string",
                "Bad Request"
              );
            }
            break;
          case "duration":
            if (typeof value[i].createTime !== "string") {
              throw new BadRequestException(
                "property text'createTime'of the objects in the array must be of type number",
                "Bad Request"
              );
            }
            break;
          case "delay":
            if (typeof value[i].mark !== "boolean") {
              throw new BadRequestException(
                "property text'mark'of the objects in the array must be of type number",
                "Bad Request"
              );
            }
            break;
          default:
            break;
        }
      }
    }
    return value;
  } else {
    throw new BadRequestException(
      "text must be an array or string",
      "Bad Request"
    );
  }
}

TextObjType的申明也须要进行绝对应的批改,如下所示:

  • 全副变为可选参数,参数的必传与否曾经在校验函数中解决了
  • 类型全副变为 any
export type TextObjType = {
  content?: any;
  createTime?: any;
  mark?: any;
};

有一部分开发者可能比拟蛊惑,不是说 ts 用 any 是可耻行为吗,这我就要纠正下你了,既然它存在天然有应用场景。在我这个场景中,对象里所有 key 的类型校验都手动解决了,如果在此处定义了它的类型,在校验函数中就会报黄色正告,因而针对于须要手动校验类型的场景而言,应用 any 是最合适的。

后果校验

最初,咱们针对于代码里定义的异样规定来验证下其是否能失常工作,如下所示:

# text 字段为 string 类型
{
    "id":"122211",
    "title":"新的题目",
    "text":"新替换的文本内容",
    "name":"新的名字",
    "config":"var config = {\"name\":\"aa\",\"age\":\"21\",\"title\":\" 题目测试 \"}"
}
>>> 接口调用胜利

# text 字段为 Array 类型所有 key 都存在
{
    "id":"122211",
    "title":"新的题目",
    "text":[{"content":"新文本","createTime":"2022-04-20","mark":false}],
    "name":"新的名字",
    "config":"var config = {\"name\":\"aa\",\"age\":\"21\",\"title\":\" 题目测试 \"}"
}

>>> 接口调用胜利

# text 字段短少 content
{
    "id":"122211",
    "title":"新的题目",
    "text":[{"createTime":"2022-04-20","mark":false}],
    "name":"新的名字",
    "config":"var config = {\"name\":\"aa\",\"age\":\"21\",\"title\":\" 题目测试 \"}"
}
>>> 接口报错 400:property text objects in the array must contain 'content'

# text 字段为 number 类型
{
    "id":"122211",
    "title":"新的题目",
    "text":19,
    "name":"新的名字",
    "config":"var config = {\"name\":\"aa\",\"age\":\"21\",\"title\":\" 题目测试 \"}"
}
>>> 接口报错 400:text must be an array or string

# text 字段短少 createTime 与 mark
{
    "id":"122211",
    "title":"新的题目",
    "text":[{"content":"新文本"}],
    "name":"新的名字",
    "config":"var config = {\"name\":\"aa\",\"age\":\"21\",\"title\":\" 题目测试 \"}"
}
>>> 接口调用胜利

如下图所示,咱们列举一个 text 字段为数字时的报错截图,运行后果合乎预期,文章结尾的问题胜利解决🤗

示例代码

文中所举代码的完整版请移步:

  • AppDto.ts-@Transform
  • JsonDataVerifyUtilas.ts-checkTitleKey
  • TextObjType.ts

写在最初

至此,文章就分享结束了。

我是 神奇的程序员,一位前端开发工程师。

如果你对我感兴趣,请移步我的集体网站,进一步理解。

  • 文中如有谬误,欢送在评论区斧正,如果这篇文章帮到了你,欢送点赞和关注😊
  • 本文首发于神奇的程序员公众号,未经许可禁止转载💌
正文完
 0