Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question: array of numbers with null values #2474

Open
BUONJG opened this issue May 6, 2024 · 1 comment
Open

question: array of numbers with null values #2474

BUONJG opened this issue May 6, 2024 · 1 comment
Labels
type: question Questions about the usage of the library.

Comments

@BUONJG
Copy link

BUONJG commented May 6, 2024

How to validate an array of numbers where null is allowed?

I have the following command:

export class MyCommand {
    @IsNotEmpty()
    @IsArray()
    @IsNumber(undefined, { each: true })
    @ArrayMaxSize(2)
    public thicknessRange: number[] = [];
}

I want to allow in thicknessRange property any of those values:

  • [1, 2] => OK
  • [1] => OK
  • [null, 2] => Not OK as null is not a number!

The problem:
I don't see any way to allow null in my array of numbers.
Usually, in order to allow null on a number, I add @IsOptional() decorator but in this case it make the entire array optional and it is not what I want.
I tried with @IsOptional({ each: true }) but it didn't work...

@BUONJG BUONJG added the type: question Questions about the usage of the library. label May 6, 2024
@ytetsuro
Copy link
Contributor

ytetsuro commented May 9, 2024

I think resolving this issue with your current approach might be difficult.
This is because you are using IsNumber rules, which validate that each item in the array is a number.
I believe creating custom rules would be necessary to address your concerns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Questions about the usage of the library.
Development

No branches or pull requests

2 participants