Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nestjs - Validating and Converting Query params [duplicate]

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;
}

When I make a get request to http://localhost:3000/schedules?companyId=3&professionalId=1

I get unexpected errors:

{
    "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"
            }
        }
    ]
}
like image 553
André Oliveira Avatar asked Jul 02 '26 16:07

André Oliveira


2 Answers

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;
}

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.

like image 200
Kim Kern Avatar answered Jul 05 '26 05:07

Kim Kern


You could also try setting enableImplicitConversion as true in your ValidationPipe.

app.useGlobalPipes(new ValidationPipe({
  transform: true,
  transformOptions: {
    enableImplicitConversion: true, // <- This line here
  },
}));

This should work locally e.g., in a Controller or method as well.

like image 21
João Carmo Avatar answered Jul 05 '26 05:07

João Carmo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!