Using class validator's IsNumberString along with number validators

See original GitHub issue

I’m submitting a…


[ ] Regression 
[ ] Bug report
[ ] Feature request
[X] Documentation issue or request
[X] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

This fails because regionId is a string when called.

import { IsInt} from 'class-validator';

export class GetLeaderboardDto {
  @IsInt()
  public readonly regionId: number;
}

Expected behavior

Having a possibility to parse it as number inbetween, like this:

import { IsInt, IsNumberString } from 'class-validator';

export class GetLeaderboardDto {
  @IsNumberString()
  @ParseNumber()
  @IsInt()
  @Min(0)
  @Max(100)
  public readonly regionId: number;
}

What is the motivation / use case for changing the behavior?

I am using a DTO class for each of my endpoints (e. g. GET requests with params) where I want to validate the input as strictly as possible.

Currently I can not use the more advanced number validitions (such as Min() IsInt() etc.) as my DTO always gets a string. I am aware I can use IsNumberString() but due to the fact that the DTO always has a string and doesn’t get the parsed number.

Does my “feature request” make sense or are there any other workarounds which are appropriate for my use case?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
iamsteevecommented, Apr 23, 2019

I think this should be in the documentation, it helped me a lot. 💃 this is my little example 😄

import { IsNumber, IsNumberString, IsString } from 'class-validator';
import { Type } from 'class-transformer';

export class QueryUsersDTO {
    @Type(() => String)
    @IsString()
    readonly companyID: string;
    @Type(() => Number)
    @IsNumber()
    readonly limit: number;
    @Type(() => Number)
    @IsNumber()
    readonly currentPage: number;
}

2reactions
weecocommented, Dec 8, 2018

@kamilmysliwiec You were right. I have to explicitly cast the Number type on my own. I thought it would work on primitives by default. Intereseting observation however:

  @IsNumberString()
  @Type(() => Number)
  @IsInt()
  public readonly regionId: number;

When I now pass 0 as parameter for the regionId, it would fail because it first tries to cast the 0 to a number, which does succeed. Then it runs the IsNumberString() validation which fails because now it as number and not a string anymore.

I wished it would run these validation checks in the order I put them in there, but it’s not a big deal. The type cast doesn’t fail for non numbers, so that I will use it rather as “type assertion”:

  @Type(() => Number)
  @IsInt()
  public readonly regionId: number;

This one will fail with input “abc” because it’s not an int.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validation | NestJS - A progressive Node.js framework
The ValidationPipe makes use of the powerful class-validator package and its declarative ... export class FindOneParams { @IsNumberString() id: number; } ...
Read more >
Auto-validation - NestJS - Netlify
We do this using decorators provided by the class-validator package, described in detail ... export class FindOneParams { @IsNumberString() id: number; } ...
Read more >
class-validator validate union string | number - Stack Overflow
You can create a custom validator for that: @ValidatorConstraint({ name: 'string-or-number', async: false }) export class IsNumberOrString ...
Read more >
How to use the class-validator.Min function in class-validator
import { Max, Min } from 'class-validator'; import { ArgsType, Field, Int } from 'type-graphql'; @ArgsType() export class RecipesArgs { @Field(type => Int) ......
Read more >
class-validator - npm
Decorator-based property validation for classes.. Latest version: 0.14.0, last published: 16 days ago. Start using class-validator in your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found