前言
在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
写在最初
至此,文章就分享结束了。
我是神奇的程序员,一位前端开发工程师。
如果你对我感兴趣,请移步我的集体网站,进一步理解。
- 文中如有谬误,欢送在评论区斧正,如果这篇文章帮到了你,欢送点赞和关注
- 本文首发于神奇的程序员公众号,未经许可禁止转载