Using class validator's IsNumberString along with number validators
See original GitHub issueI’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:
- Created 5 years ago
- Comments:14 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think this should be in the documentation, it helped me a lot. 💃 this is my little example 😄
@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:
When I now pass
0as parameter for the regionId, it would fail because it first tries to cast the0to a number, which does succeed. Then it runs theIsNumberString()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”:
This one will fail with input “abc” because it’s not an int.