Skip to content

feature: ValidateIfElse decorator for conditional custom validation with selective validator ignoring #2631

@itamar-gerasy

Description

@itamar-gerasy

Description

Currently, @ValidateIf allows ignoring all validators on a property based on a condition, but there's no built-in way to conditionally apply custom validation logic while selectively ignoring other validators.

Developers often need a pattern where:

  • If condition A is true → apply custom validation logic AND ignore all other validators
  • If condition A is false → skip custom validation AND allow other validators to run

Current workarounds are problematic:

  1. Using @ValidateIf inside custom validators ignores the custom validator itself
  2. Manually manipulating validation metadata leads to complex, error-prone code
  3. No clean way to achieve "either custom validation OR standard validators, but not both"

Use case example:

class User {
  @ValidateIfElse(
    (obj) => obj.isAdmin,           // condition
    (value) => value.length > 10,   // custom validation when admin
    { message: 'Admin names must be longer than 10 chars' }
  )
  @IsNotEmpty()  // ignored when isAdmin=true, applied when isAdmin=false
  @MinLength(3)  // ignored when isAdmin=true, applied when isAdmin=false
  name: string;
  
  isAdmin: boolean;
}

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