Code Monkey home page Code Monkey logo

Comments (4)

chanlito avatar chanlito commented on May 27, 2024 2

@Transform(x => +x) add this on your property, internally it's uses class-transformer before doing validation so this would make it to a number be it validates.

from nestjs-extensions.

chanlito avatar chanlito commented on May 27, 2024

Yes should work just fine.

from nestjs-extensions.

chanlito avatar chanlito commented on May 27, 2024

For example:

// playground.dto.ts

import { Transform } from 'class-transformer';
import {
  IsInt,
  IsNotEmpty,
  IsOptional,
  IsString,
  Max,
  Min
} from 'class-validator';
import { Dto } from 'nestjs-extensions';

@Dto()
export class PostDto {
  @IsNotEmpty()
  @IsString()
  title!: string;

  @IsString()
  @IsOptional()
  description?: string;

  @IsNotEmpty()
  @Transform(x => +x)
  count!: number;
}

@Dto()
export class QueryDto {
  @Min(1)
  @Max(20)
  @Transform(x => +x)
  limit!: number;

  @Min(0)
  @Transform(x => +x)
  offset!: number;
}

@Dto()
export class ParamsDto {
  @IsInt()
  @Transform(x => +x)
  id!: number;

  @IsInt()
  @Transform(x => +x)
  postId!: number;
}
// playground.controller.ts

import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';

import { ParamsDto, PostDto, QueryDto } from './playground.dto';

@Controller('playground')
export class PlayGroundController {
  @Post()
  async post(@Body() { title, description, count }: PostDto) {
    return { title, description, count };
  }

  @Get()
  async get(@Query() { limit, offset }: QueryDto) {
    return {
      limit: { value: limit, type: typeof limit },
      offset: { value: offset, type: typeof offset }
    };
  }

  @Get(':id/posts/:postId')
  async getOne(@Param() { id, postId }: ParamsDto) {
    return {
      id: { value: id, type: typeof id },
      postId: { value: postId, type: typeof postId }
    };
  }
}

from nestjs-extensions.

chriszrc avatar chriszrc commented on May 27, 2024

It works thanks! And it appears it's even faster than what I was doing without it, nice!

I actually want the whole dto to pass to a service, so I'm using it like this:

@Get('/:x/:y/:z')
  async findByTileDto(@Param() createTileDto: CreateTileDto, @Res() res:Response) {...}

However, even though my entity looks like this:

@Dto()
export class CreateTileDto {
  @IsNotEmpty()
  @IsInt()
  x: number;

  @IsNotEmpty()
  @IsInt()
  y: number;

  @IsNotEmpty()
  @IsInt()
  z: number;
}

I get back a 422, with a message that my values aren't numbers. And of course, it's right, they're strings, since they're coming from params. Is there a way to use the ParseIntPipe here, or am I missing something else?

from nestjs-extensions.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.