美文网首页
第四步:加入请求参数校验

第四步:加入请求参数校验

作者: 空格x | 来源:发表于2023-11-19 15:07 被阅读0次

    第一步:创建一个基础项目
    第二步:创建写接口的模块,建立moogodb数据库连接,写添加与查询接口
    第三步:加入Swagger文档

    • 注:不要关心注释代码,那是属于后面功能的区域。因为随着代码体量加大,功能不再明确,只需按照步骤并参考效果图,把关键代码写入即可,所以下只写关键代码,具体请看效果图。
      项目地址

    1 引入管道,并全局启用

    npm i --save class-validator class-transformer
    
    ~/src/main.ts
    
    import { ValidationPipe } from '@nestjs/common';
    
    // 全局挂载管道
    app.useGlobalPipes(new ValidationPipe());
    
    效果图

    2 针对请求参数文档user_copy.swagger.validator.ts文件内的代码修改

    • 加入@IsNotEmpty() 即可开启请求参数校验
    ~/src/user_copy/user_copy.swagger.validator.ts
    
    // Swagger 文档配置 https://docs.nestjs.cn/8/recipes?id=swagger
    import { ApiProperty } from '@nestjs/swagger';
    // 请求参数验证(自动验证) https://docs.nestjs.cn/8/techniques?id=%e8%87%aa%e5%8a%a8%e9%aa%8c%e8%af%81
    import { IsNotEmpty } from 'class-validator'
    
    export class userResponsesValidator {
      // 接口文档展示信息 description 示例值  example 字段描述  required:true 则展示为必填
      @ApiProperty({ description: '', example: '账号', required: true })
      @IsNotEmpty({message:'账号必填'})
      user_name: string;
    
      @ApiProperty({ description: '', example: '密码', required: true })
      @IsNotEmpty({message:'密码必填'})
      password: string;
    
      @ApiProperty({ description: '', example: '年龄', required: false })
      age: string;
    
      @ApiProperty({ description: '', example: '性别', required: false })
      sex: string;
    
      @ApiProperty({ description: '', example: '时间', required: false })
      date: string;
    }
    
    export class GetRequestDto {
      @ApiProperty({ description: '', example: '账号', required: true })
      @IsNotEmpty({message:'账号必填'})
      user_name: string;
    }
    

    3 引入并使用

    ~/src/user_copy/user_copy.controller.ts
    
    import { userResponses, userResponsesValidator, GetRequestDto } from './user_copy.swagger.validator'
    
     // ----------------------- 以下示例接口将加入Swagger文档,开启请求参数必填校验-----------------------
    
      // Swagger标签
      @ApiTags('有Swagger文档/开启请求参数校验')
      // 请求方式:Post, 请求路径:/user-copy/addValidator
      @Post('addValidator')
      // @Body() 装饰器
      async addDataValidator(@Body() body: userResponsesValidator) {
        // create 插入一条数据,直接将接收到的body参数插入
        const data = await this.userModel.create(body)
        if (data) {
          return { code: 200, data: null, message: "操作成功" }
        }
      }
    
      // Swagger标签
      @ApiTags('有Swagger文档/开启请求参数校验')
      // 针对Get请求 使其可以在接口文档模拟传参
      @ApiQuery({ name: 'user_name' })
      // 请求方式:Get, 请求路径:/user-copy/findValidator
      @Get('findValidator')
      // 响应参数文档
      @ApiCreatedResponse({ description: '', type: userResponses })
      // @Query() 装饰器
      async findDataValidator(@Query() query: GetRequestDto) {
        // find  查询指定数据
        const data = await this.userModel.find({ user_name: query.user_name })
        // 模糊查询
        // $regex 是 MongoDB 查询操作符,用于指定正则表达式进行匹配。$options: 'i' 表示不区分大小写,即忽略关键字的大小写。
        // const data = await this.userModel.find({ user_name: { $regex: query.user_name, $options: 'i' } })
    
        return { code: 200, data: data, message: "操作成功" }
      }
    
    效果图

    4 测试拦截效果

    • 现在账号/密码字段已加入必填校验,那么试试不传会发生什么
      效果图
      效果图

    相关文章

      网友评论

          本文标题:第四步:加入请求参数校验

          本文链接:https://www.haomeiwen.com/subject/vgzewdtx.html