Skip to content

class validator @RequiredIf or @OptionalIf() #2510

Open
@samislam

Description

@samislam

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()?

Metadata

Metadata

Assignees

No one assigned

    Labels

    flag: needs discussionIssues which needs discussion before implementation.type: featureIssues related to new features.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions