ValidationPipe()在Nestjs/Crud中的覆盖@Query上不起作用 [英] ValidationPipe() does not work on override @Query in Nestjs/Crud

查看:106
本文介绍了ValidationPipe()在Nestjs/Crud中的覆盖@Query上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证get请求查询中的参数,但是由于某些原因,验证管道无法识别查询的元素.

I'm trying to validate the parameters that come in the query of a get request, but for some reason, the validation pipe is unable to identify the elements of the query.

import {
  Controller,
  Post,
  Query,
  Body,
  UseInterceptors,
  Param,
  Res,
  Logger,
} from '@nestjs/common';
import { Crud, CrudController, Override } from '@nestjsx/crud';

import { OpenScheduleDto } from './open-schedule.dto';
@Crud(Schedule)
export class ScheduleController
          implements CrudController<ScheduleService, Schedule> {
          constructor(public service: ScheduleService) {}

          get base(): CrudController<ScheduleService, Schedule> {
            return this;
          }

          @Override()
          async getMany(@Query() query: OpenScheduleDto) { 
             return query; 
         } 
    }

OpenSchedule.dto

import { IsNumber, IsOptional, IsString } from 'class-validator';
export class OpenScheduleDto {

  @IsNumber()
  companyId: number;

  @IsNumber()
  @IsOptional()
  professionalId: number;

  @IsString()
  @IsOptional()
  scheduleDate: string;
}

当我请求获取 http://localhost:3000/schedules吗?companyId = 3& professionalId = 1

我收到意外错误:

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": [
        {
            "target": {
                "companyId": "3",
                "professionalId": "1"
            },
            "value": "3",
            "property": "companyId",
            "children": [],
            "constraints": {
                "isNumber": "companyId must be a number"
            }
        },
        {
            "target": {
                "companyId": "3",
                "professionalId": "1"
            },
            "value": "1",
            "property": "professionalId",
            "children": [],
            "constraints": {
                "isNumber": "professionalId must be a number"
            }
        }
    ]
}

推荐答案

这是因为当您使用 @Query 参数时,所有内容都是字符串.它没有数字或布尔值作为json之类的数据类型.因此,您必须首先将您的价值转换为数字.为此,您可以使用 class-transformer @Transform :

That is because when you use @Query parameters, everything is a string. It does not have number or boolean as data types like json. So you have to transform your value to a number first. For that, you can use class-transformer's @Transform:

import { IsNumber, IsOptional, IsString } from 'class-validator';
import { Transform } from 'class-transformer';
export class OpenScheduleDto {

  @Transform(id => parseInt(id))
  @IsNumber()
  companyId: number;

  @Transform(id => id ? parseInt(id) : id)
  @IsNumber()
  @IsOptional()
  professionalId?: number;

  @IsString()
  @IsOptional()
  scheduleDate?: string;
}


不过请注意,这是不安全的,因为 parseInt('5abc010') 5 .因此,您可能需要在转换函数中进行其他检查.


Note though, that this is unsafe because e.g. parseInt('5abc010') is 5. So you might want to do additional checks in your transformation function.

这篇关于ValidationPipe()在Nestjs/Crud中的覆盖@Query上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆