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

class validator @RequiredIf or @OptionalIf() #2510

Open
samislam opened this issue Jul 26, 2024 · 1 comment
Open

class validator @RequiredIf or @OptionalIf() #2510

samislam opened this issue Jul 26, 2024 · 1 comment
Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.

Comments

@samislam
Copy link

I have this class

class A {
  @IsString()
  @MaxLength(99)
  prop1: string 

  @IsBoolean()
  prop2: boolean
}

I want it to be:

  1. string.
  2. required (if prop2 is true).
  3. optional if prop2 is false.

In class-validator, there's is the @ValidateIf()

class A {
  @IsString()
  @MaxLength(99)
  @ValidateIf((obj) => obj.prop2)
  prop1: string

  @IsBoolean()
  prop2: boolean
}

It's true that ValidateIf() will make prop1 optional when prop2 is false. But it will disable my checking for @MaxLength() and for @IsString(). Now you can pass a number! that's not what I want. I just want it to be optional.

I was thinking, Isn't there a way to say @IsRequiredIf() or @IsOptionalIf() instead of saying @ValidateIf()?

@samislam samislam added flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features. labels Jul 26, 2024
@anasanzari
Copy link

This has been created and ignored: #932
smh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.
Development

No branches or pull requests

2 participants